I want buttons and other interactive matplotlib objects to appear from within my ipython notebook.
Here is what I've done:
Installed http://datasciencetoolbox.org, it is a vagrant box with ipython installed and version 1.3.1 of matplotlib.
I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. What's new in Matplotlib 1.4.1
I needed to run sudo apt-get install pkg-config and
sudo pip install matplotlib --upgradein order to get that going.
Then, in order to produce the nice (i.e. error-free) screenshot below, I went into the .ipython/dst-profile/ipython_notebook_config.py file and erased the line about IPKernelApp.pylab='inline' to be able to run the matplotlib.use('nbagg') command.
Then I was able to create the screenshot below. However, things still look poor. Those buttons are not buttons. That is an image of buttons. Please advise on how to make those buttons come to life!
Oh... and check this out if this helps you help me.
Thanks!
Basically you are facing two issues
the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook) to load the nbagg backend instead of your %pylabcall.
once you enabled the nbagg backend you will need to explicitly show it, ie. add a plt.show() call at the end of your script -> Update: with IPython 2.3.1 this is no longer needed (thanks #tcaswell for the hint)
With this you get the interactive matplotlib experience embedded in the IPython notebook. However, a quick try of your code does't yield to the desired result. The Button reacts and the callback is executed but the print call doesn't show anything. Anyway, to see that it's working try the following simple example (requires IPython 2.3):
%matplotlib nbagg
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
def callback(event):
plt.text(event.xdata, event.ydata, 'clicked')
f,a = plt.subplots(1)
b1 = Button(a,'Button1')
b1.on_clicked(callback)
plt.show()
Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.
Related
There are many resources that explains why %matplotlib inline is necessary to display plots inline. E.g. Purpose of %matplotlib inline. However, I feel that it is no longer necessary if we are using later version of IPython in your Jupyter Notebook. This is because I can display plots inline with or without running %matplotlib inline (each time I restart my kernel, IPython version I am using is 7.17.0). My hunch is that perhaps inline backend is activated by default for recent versions.
When I run %matplotlib to check the current backend on a new session, it says Qt5Agg. After running %matplotlib inline, when I check again by running %matplotlib, it displays the same Qt5Agg. This makes me think that %matplotlib inline is redundant as it's not changing anything. I haven't changed any IPython config myself btw.
However, I don't see any official documentation saying that inline backend is activated by default for IPython versions x.x.x+. I found this and this Github issue that was close to what I was trying to find but it doesn't fully confirm "You no longer need to run %matplotlib inline if your IPython version is x.x.x+ as it is the default behaviour". I looked through IPython recent release notes, but doesn't seem to confirm the hypothesis.
Is my hunch right? If so, what IPython versions it is not required? Is there any official documentation saying that? If not, how come I am able to plot inline without running %matplotlib inline?
This may seem as a possible duplication of Why don't I need “%matplotlib inline” in my jupyter notebook?. I wasn't able to confirm my hunch from this thread.
The answer is basically no.
There's a fine bug report that explains why.
The only ones who would need to do it are users of the non-object-oriented interface of matplotlib. Users who do not use pyplot.
If you import pyplot with the standard import matplotlib.pyplot as plt, or even if you import pandas, then it isn't necessary to execute %matplotlib inline.
The only reason %matplotlib inline is used is to render any matplotlib diagrams even if the plt.show() function is not called.
However, even if %matplotlib inline is not used, Jupyter will still display the Matplotlib diagram as an object, with something like matplotlib.lines.Line2D object at 0x0392A9D0 appearing before it in the console.
The end point is that it is not necessary anymore, however, it is still convention to keep your code clean and call on the plot that you made, and definitely recommended.
I have looked at this question and also this one. It looks like for me, matplotlib.pyplot.show() shows a figure from python, but not from jupyter console.
matplotlib.matplotlib_fname() returns the same matplotlibrc file location for both.
However, when I try to find the backend being used with matplotlib.rcParams['backend'] jupyter console tells me - 'module://ipykernel.pylab.backend_inline', regardless of which backend I have modified the matplotlibrc file to use.
Python on the other hand, correctly shows the backend I'm using; currently 'TkAgg'.
I installed matplotlib using python -mpip install -U matplotlib.
I'm using the following versions:
Windows 10
Jupyter console 5.2.0
Python 2.7.14
IPython 5.5.0
I can make do with using python, but it would be nice to figure this out for jupyter console as well.
First note that plt.show() works as expected, also in Juypter.
This uses the default 'module://ipykernel.pylab.backend_inline' backend. This backend is set by Jupyter, independently of the rcParams setting.
You may set the backend using matplotlib.use()
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
or just using IPython magic %matplotlib backendname
%matplotlib tk
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
You may change the backend using pyplot.switch_backend()
plt.switch_backend("TkAgg")
plt.plot([1,2,3])
plt.show()
or using the same IPython magic
%matplotlib tk
plt.plot([1,2,3])
plt.show()
If you want to set the backend to be used by default, see this question:
Change default backend for matplotlib in Jupyter Ipython
The plots are not displayed inline in notebooks when using matplotlib
The plots appear completely blank. Any ideas?
Came across the same issue on calling "Restart & Run all". Following this
%matplotlib notebook showing a blank histogram
I could resolve the issue adding
%matplotlib inline
at the beginning of the cell. Also note that you can prevent string output by adding ; to the end of a line.
I faced a similar issue with my Chrome Browser, whereas it works fine with Mozilla.
%matplotlib inline will lead to static images of your plot embedded in the notebook, so that cannot be the solution for this prevailing issue.
If you're trying to create a graph using the Object-Oriented Interface try adding
fig, ax = plt.subplots()
above your ax.plot
Had a similar issue - %matplotlib notebook command would not display the plot but %matplotlib inline would. Had to downgrade matplotlib version from 3.1.3 to 3.1.2.
Python 3.7.9, conda 4.5.11
Python 2.7.12
matplotlib 1.5.1
Seaborn 0.8.0
Ubuntu 16.04, with the pip package manager.
I have tried running the codes in seaborn gallery.
They simply do not work when I run a script from terminal. They just say
"seaborn.axisgrid.FacetGrid object at 0x7fdfa554c1d0". Matplotlib works though.
Seaborn does work, however, from the jupyter notebook in my browser. But even then, the plots are really bland and simple, not many interactive customisation options. Nothing like the attractive, feature rich and aesthetically pleasing things you see on tutorials, or the gallery.
Can someone tell me what's wrong?
To show a plot, you need to call
plt.show()
where plt is import matplotlib.pyplot as plt.
I am having trouble with plots in a Jupyter Notebook in Python 3.5 on Mac OSX. The following code will hang when executed:
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
myfig = plt.plot(range(5))
plt.show()
If I restart the kernel and un-comment '%matplotlib inline', I do get plots to work inline. However, I'd like to be plotting in a separate window.
If I insert the following code at the beginning:
import matplotlib
matplotlib.use('Agg')
then restart the kernel and run, the code will not hang, but nothing will be plotted, no window opened.
Details:
Mac Book Pro running OSX El Capitan
Anaconda Python 3.5 in a Jupyter Notebook
backend is "MacOSX".
There is a post on GitHub mentioning that using Qt4Agg as backend worked...
If it is not available (and if you can), you might want to try using Hombrew to install Python (instead of Anaconda), Qt and/or Gtk with which you'll be able to use matplotlib without problem.