Using Matplotlib via the OO API is easy enough for a non-interactive backend:
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')
But if I try and repeat something similar with interactive backends, I fail miserably (I can’t even get the interactive figure to appear in the first place). Does anyone have any examples of using Matplotlib via the OO API to create interactive figures?
Well, you need to be using a backend that supports interaction!
backend_aggis not interactive.backend_tkagg(or one of the other similar backends) is.Once you’re using an interactive backend, you need to do something more like this:
Honestly, though, this is not the way to use the oo interface. If you’re going to need interactive plots, do something more like this:
You’re still using the oo interface, you’re just letting
pyplothandle creating the figure manager and enter the gui mainloop for you.