using interactive and non-interactive backends within one program

backend, matplotlib

Solution

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

Problem

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?

Original source