Search code examples
pythonmatplotlibplotsubplotlinestyle

dot dashed line touch yaxis min and max in matplotlib


Is there a way to get a vertical dot-dashed line in matplotlib to always touch the top and bottom of the yaxis? I am drawing two vertical lines, with space between them, and I want them to touch the top and bottom of my yaxis. They touch the bottom of the yaxis, but they will only touch the top of the yaxis of my plot if I change the starting y-value so the linestyle pattern happens to touch at the top. I also tried using ax.vlines and got the same result.

Maybe - Is there a way to change the spacing of the dot and dash in the linestyle in order to do this?

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')

Solution

  • If I understand correctly your question, you can solve this by changing the order of how the second line is drawn, to be drawn from top to bottom

    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')