dot dashed line touch yaxis min and max in matplotlib

750 Views Asked by At

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

There are 2 best solutions below

0
On BEST ANSWER

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

You can use coordinate transformations (transformations tutorial). In order to draw a line from the bottom to the top at a certain x coordinate:

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)