gpt4 book ai didi

python - 在 matplotlib 的图中添加与 3 个不同轴相交的垂直线

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:28 25 4
gpt4 key购买 nike

我必须在图表中绘制 3 个不同 channel 的脑电图数据。我想将它们全部绘制在一个图中,并用水平线分隔。所有 channel 共用的 X 轴。

我可以使用 add_axes 轻松完成此操作。但我想画一条与这些轴相交的垂直线。但我做不到。

目前,我的示例代码如下所示。

from pylab import figure, show, setp
from numpy import sin, cos, exp, pi, arange

t = arange(0.0, 2.0, 0.01)
s1 = sin(2*pi*t)
s2 = exp(-t)
s3 = 200*t



fig = figure()
t = arange(0.0, 2.0, 0.01)

yprops = dict(rotation=0,
horizontalalignment='right',
verticalalignment='center',
x=-0.1)

axprops = dict(yticks=[])

ax1 =fig.add_axes([0.1, 0.5, 0.8, 0.2], **axprops)
ax1.plot(t, s1)
ax1.set_ylabel('S1', **yprops)

axprops['sharex'] = ax1
#axprops['sharey'] = ax1
# force x axes to remain in register, even with toolbar navigation
ax2 = fig.add_axes([0.1, 0.3, 0.8, 0.2], **axprops)

ax2.plot(t, s2)
ax2.set_ylabel('S2', **yprops)

ax3 = fig.add_axes([0.1, 0.1, 0.8, 0.2], **axprops)
ax3.plot(t, s3)
ax3.set_ylabel('S3', **yprops)




# turn off x ticklabels for all but the lower axes
for ax in ax1, ax2:
setp(ax.get_xticklabels(), visible=False)

show()

我希望我的最终图像如下所示。在我当前的输出中,我可以获得没有绿色垂直线的相同图表。

enter image description here

有人可以帮忙吗???我不想使用子图,也不想为每个轴添加 axvline 。

谢谢你,托塔德里

最佳答案

使用

vl_lst = [a.axvline(x_pos, color='g', lw=3, linestyle='-') for a in [ax1, ax2, ax3]]

更新每一帧:

new_x = X
for v in vl_lst:
v.set_xdata(new_x)

axvline doc

关于python - 在 matplotlib 的图中添加与 3 个不同轴相交的垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16682802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com