What does Jupyter notebook call when it presents a matplotlib image? - matplotlib

In Jupyter, if the last statement in a cell returns a matplotlib.Figure, the figure is displayed.
One can perform a similar trick with custom classes by providing a _repr_png_ function in that class. However, matplotlib Figures do not provide this function. This makes me wonder. How does Jupyter "know" what to do with a matplotlib figure?

How does Jupyter "know" what to do with a matplotlib figure?
In the inline backend, this is achieved by registering a function to show changed figures (flush_figures) with the post_execute event in IPython, see configure_inline_support.

Related

pop-up plots using Python Jupyter Notebook

Is there a way to have the plots created inside Jupyter Notebook using matplotlib to appear on a separate pop-up screen that would allow you to expand/shrink the image by hand? I've tried experimenting with (%matplotlib notebook) but that didn't really do the trick.
Just wondering if this is possible.
Just use an interactive backend. This works for me:
import matplotlib.pyplot as plt
%matplotlib tk
plt.plot([1, 2])
The notebook (nbagg) backend also allows for expand/shrink by hand. It has some rough edges though.
The tkinter backend is a bit buggy (windows 10, python 3).
I used %matplotlib qt for the matplotlib plot that we are all used to.

I don't know how to make a new matplotlib figure

I'm using Jupyter notebook with %matplotlib notebook in one of the first lines.
When making multiple plots, I have to physically press the 'stop interaction' button on each figure before running another plot, or else the newest plot will be overlaid onto the previous figure.
I think the problem is that I'm not specifying that a new figure needs to be made for each plot? But I'm stumped as to how best to do that!
DO I REALLY HAVE TO SAY PLT.FIGURE EVERY SINGLE TIME? THIS SEEMS UNLIKELY TO ME...?
Thanks in advance!
This is a bug with the notebook backend, but luckily the person who reported it also reported a workaround
Within the Notebook you will need to add the plt.ioff after you import pyplot.
Here is a snip from the top of a notebook, that makes it work for me. I was getting plots over written like you.
%matplotlib notebook # this is to allow the plotting in the notebook
import numpy as np
from scipy.linalg import hadamard
import matplotlib.pyplot as plt
plt.ioff() # this stops the graphs from overwriting each other

When matplotlib is embedded in PyQt4 how are axes/subplots labelled without using pyplot?

It's easy with pyplot but apparently pyplot shouldn't be used when embedding. I haven't been able to find any non-pyplot embedding examples where labels are used.
Maybe you found this page, too. Maybe this example helps. I have not tested it, thus I cannot say if it works.
Staying with the example from the matplotlib example, the labels in the subplot self.axes can be set using self.axes.set_xlabel(x Label") or self.axes.set_ylabel("y Label").

Viewing graphs in Jupyter or IPython

I am trying to use both bokeh and matplotlib in my IPython notebook... Neither work perfectly.
Attached is a screen shot of Bokeh. Matplotlib explanation is below.
Here are my system specs:
-Windows 7 with Vagrant
-Jupyter/IPython
BOKEH -- buttons are static images; there is no resizing, yet the graph is interactive
Should look like from this website: http://docs.bokeh.org/en/latest/docs/quickstart.html
MATPLOTLIB -- only static shots appear when it should be zoomable, etc (like bokeh)
1) read the documentation.
you would only change output_file() to a call to output_notebook() instead.
Which you did not seem to do above.
2) Why should it ? What di you do to make it zoomable ?
3) try not to post 2 unrelated question at the same time.
You appear to be using an older version of Bokeh. The issue with the CSS problems (button appearance) has been fixed for some time. As mentioned above, you will need to execute output_notebook() to load Bokeh for IPython notebook usage. For future reference, questions like this greatly benefit from providing as much information as possible (e.g., Bokeh and browser versions, platform information, etc.) Without that information it is impossible to diagnose problems with any certainty.

using interactive and non-interactive backends within one program

I am running code written with PyQt4 which uses matplotlib's Qt4Agg backend for showing live plots in windows. At the same time, I would like to use matplotlib in background thread to produce (different) figures which are only saved to file, not shown on the screen.
I can use Qt4Agg in the background thread, but I am getting a bunch of
QPixmap: It is not safe to use pixmaps outside the GUI thread
warnings, and also crashes in some cases.
As far as I see, matplotlib currently supports using only one backend at any given time (which can be changed via switch_backend, but that closes all existing figures). Is there some way to work around this limitation, and to assign per-figure backend?
To my knowledge, only if you don't use the pyplot interface.
For instance, using the full OO interface for a simple plot:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
canvas.print_figure('test.png')
HTH