Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 568
eudoxos Avatar asked Aug 13 '13 11:08

eudoxos


1 Answers

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

like image 63
pelson Avatar answered Nov 20 '22 00:11

pelson