点虚线触摸 matplotlib 中的 yaxis 最小值和最大值

dot dashed line touch yaxis min and max in matplotlib

有没有办法让matplotlib中的垂直点划线始终触及yaxis的顶部和底部?我正在画两条垂直线,它们之间有 space,我希望它们触及我的 yaxis 的顶部和底部。它们触及 yaxis 的底部,但如果我更改起始 y 值,它们只会触及我绘图的 yaxis 的顶部,因此线型模式恰好触及顶部。我也尝试使用 ax.vlines 并得到了相同的结果。

也许 - 有没有办法改变线型中点和破折号的间距来做到这一点?

plt.plot((55843.8747516981, 55843.8747516981), (yminPlot, 4.53), linewidth=2,
         linestyle='-.', color='r')
plt.plot((55843.8747516981, 55843.8747516981), (7.03, ymaxPlot), linewidth=2,
         linestyle='-.', color='r')

您可以使用坐标变换 (transformations tutorial)。为了在某​​个x坐标处从下到上画一条线:

import matplotlib.transformas as transforms

# get current axes
ax = plt.gca()

# define a blended transformation
#   ax.transData ... use data coordinates
#   ax.transAxes ... use axes coordinates ranging from (0,0) to (1,1)
trans = transforms.blended_transform_factory( ax.transData, ax.transAxes )

# plot a vertical line at x=55843.8747516981
# note that the 
plt.plot( (55843.8747516981,55843.8747516981), (0,1), linewidth=2, linestyle='-.', color='r', transform=trans)

如果我理解正确你的问题,你可以通过改变第二条线的绘制顺序,从上到下绘制来解决这个问题

plt.plot((55843.8747516981, 55843.8747516981), (yminPlot, 4.53), linewidth=2,
         linestyle='-.', color='r')
plt.plot((55843.8747516981, 55843.8747516981), (ymaxPlot, 7.03), linewidth=2,
         linestyle='-.', color='r')