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

1
  • 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. Commented Aug 30, 2017 at 7:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. You really corrected my mistakes and told me the convenient way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.