首页 > 解决方案 > 如何将子图与虚线边界而不是实线与 Matplotlib 连接?

问题描述

如何在python中用虚线而不是实线连接子图?我使用此站点https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html水平组合 3 个散点图,但它们之间有一条实线。有没有办法用虚线将它们连接起来,使它看起来像一个时间线,就像这张图片中显示的那样

标签: pythonmatplotlibscatter-plotsubplotdotted-line

解决方案


你的问题不是很清楚,但我认为你需要一种方法来绘制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);

在此处输入图像描述


推荐阅读