2

I'm using the following:

fig, ax = plt.subplots(figsize=(20, 10))

ax.set_ylim(bottom=0, top=10)
for i in range(4):
    ax.axvline(x=i, ymin=5, ymax=9, color="red", linewidth=40)

Which gives:

enter image description here

I would expect there to be a vertical line at each point from y = 5 to y = 9.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
baxx
  • 3,956
  • 6
  • 37
  • 75
  • 3
    See the [docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.axvline.html) for the values `ymin` and `ymax` expect. Use [`vlines`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.vlines.html#matplotlib.axes.Axes.vlines) instead of `axvline`. – BigBen Aug 26 '21 at 13:28

2 Answers2

3

You should use matplotlib.pyplot.vlines, as suggested by BigBen in the comment:

for i in range(4):
    ax.vlines(x=i, ymin=5, ymax=9, color="red", linewidth=40)

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80
2

If you look at the parameters for axvline, you see that ymin and ymax goes from 0 to 1. A fraction of your complete ylimit. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axvline.html

So you need something like .5 to .9 or calculate the appropriate fractions.

fig, ax = plt.subplots(figsize=(20, 10))

ax.set_ylim(bottom=0, top=10)
for i in range(4):
    ax.axvline(x=i, ymin=.5, ymax=.9, color="red", linewidth=40)

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187