如何使用虚线边界而不是实线与 Matplotlib 连接子图?

How to connect subplots with a dotted line boundary instead of a solid line with Matplotlib?

如何在 python 中用虚线而不是实线连接子图?我用过这个网站 https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html to combine 3 scatter plots horizontally but they have a solid line between them. Is there a way to join them with a dotted line so it looks like a timeline,something like shown in this image

可以通过自定义官方参考中的例子,隐藏图框的左右两边来实现。 这个例子只隐藏了框架,所以刻度线仍然存在,看起来像一条虚线。如果您对此感到满意,则可以按原样使用它。如果你想自定义它,你需要隐藏刻度并添加带有ax.vlines或类似的虚线。

import matplotlib.pyplot as plt
import numpy as np

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig = plt.figure()
gs = fig.add_gridspec(1,3, wspace=0)
axs = gs.subplots(sharex=True, sharey=True)
fig.suptitle('Sharing both axes')
axs[0].plot(x, y ** 2)
axs[1].plot(x, 0.3 * y, 'o')
axs[2].plot(x, y, '+')
axs[0].spines['right'].set_visible(False)
axs[1].spines['left'].set_visible(False)
axs[1].spines['right'].set_visible(False)
axs[1].spines['left'].set_visible(False)
axs[2].spines['left'].set_visible(False)

# Hide x labels and tick labels for all but bottom plot.
for ax in axs:
    ax.label_outer()

你的问题不是很清楚,但我认为你需要一种在情节中绘制 vlines 的方法。 这可能会有所帮助。

import numpy as np
import matplotlib.pyplot as plt

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, ax = plt.subplots()
ax.plot(x, y,'b--')
ax.vlines(x =[1,2,3,4,5,6], ymin=-1.085, ymax=1.085, colors='k', linestyles='--')
ax.grid()

或者可能是这样的:

import numpy as np 
import matplotlib.pyplot as plt

x = [1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1]
t = np.arange(0,len(x))

code_1 = (1,0,1,0,1,0)
code_0 = (0,1,0,1,0,1)
x1 = [1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0]

x2 = np.logical_xor(x1, x)

g, (ax0, ax1, ax2) = plt.subplots(3, sharex=True, sharey=True, figsize=(12,8))

ax0.step(t,x,'b', lw=3, label = "Data Signal");ax0.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=16);ax0.vlines(x =[np.arange(0,len(x))], ymin=-0.1, ymax=1.085, colors='k', linestyles='--');ax0.axes.xaxis.set_visible(False);ax0.axes.yaxis.set_visible(False);plt.grid(True)

arrow_kwargs = {'arrowprops':dict(arrowstyle="<->",lw=2.5)}
ax0.annotate(xytext=(5,1.1), xy=(11,1.1), s='', **arrow_kwargs); ax0.text(8, 1.2, '$T_{b} $', va='bottom', ha='center',fontsize=20)

ax1.vlines(x =[np.arange(0,len(x))], ymin=-0.1, ymax=1.1, colors='k', linestyles='--');ax1.step(t,x1, 'm', lw=3, label = "Pseudo-random\ncode",alpha = 0.6);ax1.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=16);ax1.axes.xaxis.set_visible(False);ax1.axes.yaxis.set_visible(False);ax1.grid()
ax2.step(t,x2, 'orange', lw=3, label = "Transmitted signal\nData XOR PN code",alpha = 0.6);ax2.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=16);ax2.axes.xaxis.set_visible(False);ax2.axes.yaxis.set_visible(False);ax2.grid()
ax2.vlines(x =[np.arange(0,len(x))], ymin=-0.1, ymax=1.1, colors='k', linestyles='--')
ax2.annotate(xytext=(8,0.1), xy=(9,0.1), s='', **arrow_kwargs); ax2.text(8.5, 0.15, '$T_{c}$', va='bottom', ha='center',fontsize=20)
g.subplots_adjust(hspace=0.1);