I generated two arrays with 10 different values. How do I plot 3 specific values within each array using matplotlib? Here is my code so far:
import numpy as np
import matplotlib as plt
x = np.array(1,2,3,4,5,6,7,8,9,10)
y = np.array(1,2,3,4,5,6,7,8,9,10)
I only want to plot the points 3,4,5 of the x-array and it's corresponding y values. I have tried this:
plt.plot(x[2,3,4], y[2,3,4])
plt.show()
But I get the error "too many indices for array." However, if I write
plt.plot(x[2], y[2])
plt.show()
the second element in the arrays will plot.
The problem is the syntax of x[3, 4, 5]
. It is wrong what you want to do is x[3]
, x[4]
, x[5]
, which are the respective elements of the array.
print(x[3], x[4], x[5]) # print 4, 5, 6
A more comfortable way to do this is:
plt.plot(x[2:5], y[2:5])
plt.show()
Where x[2:5]
returns from the third to the fifth element.
As Tony Tannous says, the creation of the array is also wrong. np.array
needs a list!
Then you also have to change the creation of x and y:
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = np.array([1,2,3,4,5,6,7,8,9,10])
Adding [
and ]
to make it a list.
Surely you should see the documentation of Indexing
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.