-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug : Need to run plt.plot() twice from a freshly started notebook to see the plot #3691
Comments
I think the issue is with doing it in the same cell where you import matplotlib - there's some setup that only happens after that cell has run. |
Does this mean that this is the expected notebook behavior? Or is this actually a bug? I can confirm that this is an issue for me, and I can also confirm that splitting out the import into a separate cell does work. (It would be preferable if this wasn't happening for a short course that I'm teaching soon-ish, which is why I ask.) |
@takluyver Hi, I added the try :
import matplotlib as mpl
import matplotlib.pyplot as plt
except :
pass Once I restart the ---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-61e052f4bd52> in <module>()
----> 1 plt.plot([1,2,3,4])
NameError: name 'plt' is not defined |
@rnelsonchem it's somewhere inbetween. I think that if someone can figure out a way to fix it that doesn't break other things, that would probably be welcome. @sebma config files don't go into your interactive namespace. Try a startup file instead. |
@takluyver Thanks, for the tip ! I'm a little confused, why are the config files shared across Is this because IPython is the Jupyter kernel for Python language ? |
Yep. IPython deals with running your Python code, Jupyter provides the notebook interface, including things like saving and loading notebook files. So there's an extra reason that putting it in |
@takluyver Thanks :-) |
Try using “%matplotlib inline”. There are some discussions on %matplotlib inline: |
@takluyver agree the fault may pertain to the cell where the matplotlib import occurs. This particularly affects libraries that lazily import matplotlib, such as xarray: In [1]: import xarray, numpy as np
In [2]: x = xarray.DataArray(np.random.random((5,5)))
In [3]: x.plot() # does not work
In [4]: x.plot() # does work I think this behaviour is counter-intuitive and undesirable. How exactly does the notebook change when matplotlib is imported? |
That's unfortunate. I can't really remember the details of what goes on here, but it involves this code in ipykernel and this code in IPython. Certainly if you fall into the There's a comment there saying that that branch is only needed for Python 2. But it's possible that other changes since that was written mean that branch is used on Python 3 as well. |
If you inspect Within one cell, the line importing For all subsequent cells, the latter callback is replaced by (Seems When It seems that the underlying bug relates to circular imports (causing that setup to initially fail). The Note that everything works if the imports occur in a different order: In [1]: import ipykernel.pylab.backend_inline # import this first
import matplotlib.pyplot as plt
plt.plot([1,3,2]) # this works *The setup mentioned earlier involves executing a couple functions in Normally, In [1]: import inspect, IPython.core.pylabtools
exec(inspect.getsource(IPython.core.pylabtools.activate_matplotlib)
.replace('import matplotlib.pyplot', 'from matplotlib import pyplot')
.replace('matplotlib.pyplot', 'pyplot'),
IPython.core.pylabtools.__dict__) # apply patch
import matplotlib.pyplot as plt
plt.plot([1,3,2]) # this works also, in python 3.6 So there are a several different ways of fixing our problem. Ideally, the imports should be fixed so the workaround isn't needed (ideally by eliminating circular imports and/or by not executing functions during import, but possibly just by using more resilient import syntaxes). Alternatively, In [1]: import matplotlib.pyplot as plt
get_ipython().events.trigger('post_run_cell') # call configure_once() earlier
plt.plot([1,3,2]) # also works I'll see about putting together a PR or two.. |
Hi,
Steps to reproduce :
Can you please have a look ?
The text was updated successfully, but these errors were encountered: