[英]How to plot specific points in array with matplotlib?
I generated two arrays with 10 different values.我生成了两个具有 10 个不同值的数组。 How do I plot 3 specific values within each array using matplotlib?
如何使用 matplotlib 在每个数组中绘制 3 个特定值? 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.我只想绘制 x 数组的 3,4,5 点及其对应的 y 值。 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]
.问题是
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.您想要做的是
x[3]
、 x[4]
、 x[5]
,它们是数组的各个元素。
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.其中
x[2:5]
从第三个元素返回到第五个元素。
As Tony Tannous says, the creation of the array is also wrong.正如Tony Tannous所说,数组的创建也是错误的。
np.array
needs a list! np.array
需要一个列表!
Then you also have to change the creation of x and y:然后你还必须改变 x 和 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当然你应该看到索引的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.