简体   繁体   中英

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?

You could do

import matplotlib.pyplot as plt

plt.plot(*result.T)

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM