Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 897
ariel Avatar asked Feb 17 '10 17:02

ariel


People also ask

Can you draw in Matplotlib?

As well a being the best Python package for drawing plots, Matplotlib also has impressive primitive drawing capablities.

What is the use of Matplotlib Pyplot plot () function?

The plot() function in pyplot module of matplotlib library is used to make a 2D hexagonal binning plot of points x, y. Parameters: This method accept the following parameters that are described below: x, y: These parameter are the horizontal and vertical coordinates of the data points. x values are optional.


2 Answers

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.

like image 133
Christian Alis Avatar answered Oct 10 '22 08:10

Christian Alis


If you are using numpy array you could extract by axis:

a = array([[1,2],[3,3],[4,4],[5,2]]) plot(a[:,0], a[:,1], 'ro') 

For lists or lists you'll need some helper, like:

a = [[1,2],[3,3],[4,4],[5,2]] plot(*sum(a, []), marker='o', color='r') 
like image 42
pprzemek Avatar answered Oct 10 '22 08:10

pprzemek