Issue
I'm new to matplotlib. I'm trying scenarios with it on jupyter notebook, but I'm confused about how it handles updating attributes on different cells. For example, I plotted coordinates in a cell:
# Works
import matplotlib.pyplot as plt
y = [-1, 2, 3.5, 10]
x = [1,2,3,4]
plt.plot(x,y)
plt.show()
Which works. However, when trying to update xlabel
and ylabel
in a further cell, an empty window is displayed:
# Does not work
plt.ylabel("Random numbers")
plt.xlabel("Index")
plt.show()
I'm not sure why it doesn't work. I tried calling plot()
without arguments before show()
, but it also displays a blank window.
The only way it works is re-passing my coordinates to plt:
# works
plt.ylabel("Random numbers")
plt.xlabel("Index")
plt.plot(x,y)
plt.show()
I found it a little weird, considering Pyplot docs states it preserve state across function calls.
The weirder for me however is that this snippet works:
# works(!)
plt.plot(x,y)
plt.ylabel("Random numbers")
plt.xlabel("Index")
plt.show()
It seems setting labels before of after plotting doesn't mater if i'm running it on the same cell. But it matters if i'm running it on different cells.
How exactly pyplot updates attributes in different jupyter notebook cells?
Solution
From the documentation:
"At the end of show()
the figure is closed and thus unregistered from pyplot."
So after using show()
your figures do not exist anymore. You can not show, then set the labels and then show again.
Answered By - dkruit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.