matplotlib axvline truth ambiguous or list issue?
No, you can't pass a list to axvline
. For multiple vertical lines within one line, something like this will do
[pylab.axvline(_x, linewidth=1, color='g') for _x in x]
Instead of multiple calls to axvline
we can use the plot command itself but providing the correct transformation (this is many times faster if there are many lines):
import matplotlib.transforms as tx
ax = pylab.gca()
trans = tx.blended_transform_factory(ax.transData, ax.transAxes)
pylab.plot(np.repeat(x, 3), np.tile([.25, .75, np.nan], len(x)), linewidth=2, color='g', transform=trans)
axvline
is for creating x vertical line.
Meaning at a certin x point from y-min to y-max.
x
cannot be a list
type.
a simple example:
axvline(x=.5, ymin=0.25, ymax=0.75)
You can read more here
If you want to create a rectangle you can use:
axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)
in your case xmin is 1 and x man is 300.
For completeness, there is also the possibility of using matplotlib.pyplot
s vlines
. This function accepts a list of x-coordinates. Furthermore, you can specify where the lines need to start/end with the arguments ymin
and ymax
. In the case of this question the code would be:
import matplotlib.transforms as mt
fig, ax = plt.subplots()
ax.plot(list, values, label='Trend', color='k', linestyle='-')
trans = mt.blended_transform_factory(ax.transData, ax.transAxes)
ax.vlines(x, ymin=0, ymax=1, linewidth=1, color='g', transform=trans)
Using the transform
argument makes it easier to have the lines from the top to the bottom of your plot. You can read more about it here. You can also skip that argument. In that case you have to specify ymin
and ymax
in actual y-coordinates.