Ravaal
Ravaal

Reputation: 3359

How do I make a numpy array's numbers plot on different axes?

What I have is a numpy array that looks like the following.

>>> result
array([[    0. ,     0. ],
       [   18.6,   -11.1],
       [   36.1,   -21.9],
       ..., 
       [ -535.5,  1020.3],
       [ -535.5,  1020.3],
       [ -535.5,  1020.3]])

And what I'm trying to do is plot it using matplotlib.pyplot as plt with the first number on the x axis and the second number on the y axis. How do I do that?

Upvotes: 1

Views: 397

Answers (2)

plonser
plonser

Reputation: 3363

You could do

import matplotlib.pyplot as plt

plt.plot(*result.T)

The * is a fancy way of unpacking a list, see here.

Upvotes: 2

BlivetWidget
BlivetWidget

Reputation: 11063

You can get slices of numpy arrays like so:

import numpy as np
a = np.array([[0, 1], [2, 3], [4, 5]])

>>> a
array([[0, 1],
       [2, 3],
       [4, 5]])

>>> a[:,0]
array([0, 2, 4])

>>> a[:,1]
array([1, 3, 5])

So if the first column has your x values, and the second has your y values, you would plot like:

import matplotlib.pyplot as plt
plt.plot(a[:, 0], a[:, 1])
plt.show()

Upvotes: 1

Related Questions