Hey folks, so far i have seen two ways of getting a matplotlib plot to show via PyScript:
- Call
matplotlib.pyplot.figure()
and store the instance infig
- Do the plotting via
plot()
calls - then type
fig
The other way i have seen working is:
import matplotlib.pyplot as plt
plt.plot(..)
plt
However, i haven’t been able to get the pylab
module working i.e. to show the graph with something like this:
from pylab import plot, show
plot(..)
show()
Now, i am guessing anything that prints inside the Python program gets shown in the div
. However, with matplotlib
, i am not able to understand why the first two options work and the third doesn’t.
After I wrote this, I came across another behavior I don’t understand. Consider the following code:
def draw_graph(x, y):
plt.plot(x, y)
plt.show()
fig = plt.figure()
try:
u = float(input('Enter the initial velocity (m/s): '))
theta = float(input('Enter the angle of projection (degrees): '))
except ValueError:
print('You entered an invalid input')
else:
# calculate x and y (omitted)
draw_graph(x, y)
fig
The above shows the graph. However, when I write the try..except..else
as follows, it doesn’t (I move the fig
statements inside the else
block):
try:
u = float(input('Enter the initial velocity (m/s): '))
theta = float(input('Enter the angle of projection (degrees): '))
except ValueError:
print('You entered an invalid input')
else:
fig = plt.figure()
# calculate x and y (omitted)
draw_graph(x, y)
fig
Would appreciate any pointers, thank you.