0

plt.show(), displays a window, but there's no curve on it.

My code:

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

Output

enter image description here

SiHa
  • 7,830
  • 13
  • 34
  • 43
J.I.S
  • 19
  • 5
  • With every call of `plt.plot`, a `Lines` object is added to the `Figure` instance. A `Lines` object tries to make lines between the points given in order (linear interpolation). Now the problem is that to make a line one needs at least two points, but with every single call of `plt.plot` you create a `Lines` object with only one point. That is why you don't see anything. Use NumPy's universal functions `sin` and `cos` instead and let them work with arrays instead. – Michael H. Aug 30 '17 at 07:51

1 Answers1

1

From what I see, the second calculation of L is exactly the same as the first one (except that you always use the same value of theta, which I guess is not what you're trying to achieve in the second loop). Also you don't use celta at all. We're just going to kick that out. Lm is not used at all, so I'm also gonna kick that out too, but from what I see you can just calculate Lm afterwards with np.max(L).

What is left is the following code:

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

Now, you have only one call of plt.plot which creates a Lines instance with a lot of points and you can see a plot.

Michael H.
  • 3,323
  • 2
  • 23
  • 31