Given coordinates of [1,5,7,3,5,10,3,6,8]
for matplotlib.pyplot
, how do I highlight or colour different segments of the line. For instance, the coordinates 1-3 ([1,5,7,3]
) in the list represent attribute a
. How do I colour this bit of the line and mark it in the legend?
Edit: The list in question contains tens of thousands of elements. I'm trying to highlights specific sections of the list. From the answers so far, is it right to assume I must draw each segment one by one? There isn't a way to say "select line segment from x1 coord to x2 coord, change colour of line"
Try this on for size:
from matplotlib import pyplot as plt
y1 = [1,5,7,3]
x1 = range(1,5)
y2 = [3,5,10,3,6,8]
x2 = range(4,len(y2)+4)
plt.plot(x1, y1, 'go-', label='line 1', linewidth=2)
plt.plot(x2, y2, 'rs--', label='line 2')
plt.legend()
plt.show()
Will give you:
Also, you ought to look at the help too, it's pretty helpful. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With