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?
As well a being the best Python package for drawing plots, Matplotlib also has impressive primitive drawing capablities.
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.
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.
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')
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