2017-08-30 168 views
0

plt.show(),显示一个窗口,但它没有曲线。Matplotlib不绘制曲线

我的代码:

In [1]: import math 

In [2]: import numpy as np 

In [3]: import matplotlib.pyplot as plt 

In [4]: Lm=0 

In [5]: for angle in np.arange(0.0,90.0,0.1): 

    ...:  theta = angle*math.pi/180 

    ...:  L=0.25*(math.cos(theta))*(5*math.sin(theta)+math.sqrt(25*math.sin(theta)*math.sin(theta)+80)) 

    ...:  print(L,"\t",angle) 

    ...:  if L>Lm: 

    ...:   Lm=L 

    ...: 


In [6]: plt.figure(1) 

Out[6]: <matplotlib.figure.Figure at 0x284e5014208> 

In [7]: for angle in np.arange(0.0,90.0,0.1): 

    ...:  celta = angle*math.pi/180 

    ...:  L=0.25*(math.cos(theta))*(5*math.sin(theta)+math.sqrt(25*math.sin(theta)*math.sin(theta)+80)) 

    ...:  plt.figure(1) 

    ...:  plt.plot(angle,L) 

    ...: 

In [8]: plt.show() 

输出

enter image description here

+0

随着'plt.plot'的每次调用,一个'Lines'对象被添加到'Figure'实例。一个'Lines'对象试图按顺序给出的点之间的线(线性插值)。现在问题在于要创建一条线需要至少两个点,但每次调用'plt.plot'时,都会创建一个只有一个点的线条对象。这就是为什么你什么都看不到。使用NumPy的通用函数'sin'和'cos'来代替它们,让它们与数组一起工作。 – Michael

回答

1

从我所看到的,L第二计算是完全一样的第一个(除了你总是使用相同的值theta,我想这不是你想要在第二个循环中实现的) 。你也根本不使用celta。我们只是将它踢出去。根本没有使用Lm,所以我也要去解决这个问题,但是从我看到的结果你可以计算出Lm之后的np.max(L)

而剩下的就是下面的代码:

import numpy as np 
import matplotlib.pyplot as plt 

angles = np.arange(0.0, 90.0, 0.1) 
theta = angles * np.pi/180 
L = 0.25 * np.cos(theta) * (5*np.sin(theta) + 
          np.sqrt(25*np.sin(theta)*np.sin(theta) + 80)) 
plt.plot(angles, L) 
plt.show() 

现在,你有plt.plot只有一个呼叫与一分不少的创建Lines例如,你可以看到一个情节。

+0

非常感谢。你真的纠正了我的错误,并告诉我方便的方法。 –