Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Segments in Matplotlib

Tags:

matplotlib

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"

like image 791
Federer Avatar asked Feb 24 '12 16:02

Federer


1 Answers

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:

enter image description here

Also, you ought to look at the help too, it's pretty helpful. :-)

like image 70
macduff Avatar answered Sep 27 '22 19:09

macduff