J.I.S
J.I.S

Reputation: 19

Matplotlib does not plot curve

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

Upvotes: 0

Views: 1290

Answers (1)

Michael H.
Michael H.

Reputation: 3483

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.

Upvotes: 1

Related Questions