draw points using matplotlib.pyplot [[x1,y1],[x2,y2]]


Question

I want to draw graph using a list of (x,y) pairs instead of using two lists, one of X's and one of Y's. Something like this:

a = [[1,2],[3,3],[4,4],[5,2]]
plt.plot(a, 'ro')

Rather than:

plt.plot([1,3,4,5], [2,3,4,2])

Suggestions?

1
37
3/6/2012 6:32:12 PM

You can do something like this:

a=[[1,2],[3,3],[4,4],[5,2]]
plt.plot(*zip(*a))

Unfortunately, you can no longer pass 'ro'. You must pass marker and line style values as keyword parameters:

a=[[1,2],[3,3],[4,4],[5,2]]
plt.plot(*zip(*a), marker='o', color='r', ls='')

The trick I used is unpacking argument lists.

57
2/23/2010 3:04:10 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon