I'm trying to plot some vertical lines on a chart that has a "list" of integers (1...300) and some "values" (floats). The following works when x=[48], but when x is set to x=[48, 83, 155, 292], the following code:
pylab.plot(list, values, label='Trend', color='k', linestyle='-')
pylab.axvline(x, linewidth=1, color='g')
Generates this error:
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2053, in axvline
ret = ax.axvline(x, ymin, ymax, **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3478, in axvline
scalex = (xx<xmin) or (xx>xmax) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
What does it mean? (I thought it was pretty funny that python pretends to knows when truth is ambiguous). Can I not pass a list to axvline?
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)