Reputation: 103
I would expect the following to generate a line centered at 60 with a width of 40, but the width is not 40 (not even 20) according to the tick labels. How do I create a vertical line of a given width using axvline?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,100,100)
y = 2*x+0.5
plt.plot(x,y)
plt.axvline(60)
plt.axvline(60, lw=40, alpha=0.5)
plt.show()
Upvotes: 3
Views: 9202
Reputation: 1677
I think lw is in pixel units while the 60 is in data coordinates. You can convert it to data units if you know the ratio between the two but you can also just use axvspan
instead to achieve the same thing.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,100,100)
y = 2*x+0.5
plt.plot(x,y)
plt.axvline(60)
width = 40
plt.axvspan(60-width/2,60+width/2, alpha=0.5)
plt.show()
Upvotes: 2