[英]Python - Not able to plot graph from a numpy array
I am trying plot a graph with a numpy array but the error occur in plt.plot(s,s)
. 我正在尝试使用numpy数组绘制图形,但错误发生在
plt.plot(s,s)
。
import numpy as np
import matplotlib.pyplot as plt
def npArrDefine():
np.array=[]
s=np.array
for i in range(10):
s.append(i+3)
plt.plot(s,s)
plt.axis([0,5,0,20])
plt.show()
npArrDefine()
There's a lot of things wrong with your code. 您的代码有很多错误。
np.array=[]
and s=np.array
. np.array=[]
和s=np.array
。 Here, you are setting a name that numpy uses to be an empty list (horrible!), and then you are setting s to be that empty list. 在这里,您将numpy用作一个空列表的名称(太糟了!),然后将s设置为该空列表。 Don't do this.
不要这样 Simply do
s=[]
. 只需执行
s=[]
。
Later on you are trying to plot by using plt.plot(s,s)
which means you want to plot s
against itself. 稍后,您尝试使用
plt.plot(s,s)
进行绘图plt.plot(s,s)
这意味着您想针对自身绘制s
。 This will always give you a straight 45 degree line with 0 intercept even if your code worked. 即使您的代码有效,这也始终会为您提供一条45度直线且截距为0的直线。
Your code block should be: 您的代码块应为:
s=[]
for i in range(10):
s.append(i+3)
s = np.array(s) #This line is optional, pyplot can use any array-like.
plt.plot(s)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.