After trying hard to send a plot to my widget, I finally found a solution that works: get the figure from the widget!
In short, it is has simple as:
from pyqtgraph.Qt import QtGui, QtCore
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure
class MatplotlibWidget(QtGui.QWidget):
“”"
Implements a Matplotlib figure inside a QWidget.
Use getFigure() and redraw() to interact with matplotlib.
Example::
mw = MatplotlibWidget()
subplot = mw.getFigure().add_subplot(111)
subplot.plot(x,y)
mw.draw()
"""
def __init__(self, size=(5.0, 4.0), dpi=100):
QtGui.QWidget.__init__(self)
self.fig = Figure(size, dpi=dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self)
self.toolbar = NavigationToolbar(self.canvas, self)
self.vbox = QtGui.QVBoxLayout()
self.vbox.addWidget(self.toolbar)
self.vbox.addWidget(self.canvas)
self.setLayout(self.vbox)
def getFigure(self):
return self.fig
def draw(self):
self.canvas.draw()
A special thanks to the pyqtgraph maintainer
http://luke.campagnola.me/code/pyqtgraph/
···
From: Emmanuel Mayssat <emayssat@…9…>
To: Emmanuel Mayssat
<emayssat@…9…>; Eric Firing <efiring@…202…>; “matplotlib-users@lists.sourceforge.net” matplotlib-users@lists.sourceforge.net
Sent: Tuesday, April 3, 2012 4:36 PM
Subject: Re: [Matplotlib-users] assigning a plot object to a figure
Although this work, I lose the position of the axes.
How can I reposition the axes2 which was at 221 in fig2, to 221 in fig1 ?
From: Emmanuel Mayssat <emayssat@…9…>
To: Eric Firing <efiring@…202…>; “matplotlib-users@lists.sourceforge.net”
matplotlib-users@lists.sourceforge.net
Sent:
Monday, April 2, 2012 3:20 PM
Subject: Re: [Matplotlib-users] assigning a plot object to a figure
Well, not sure if that is supported, but I found a way !:
[...]
self.fig1 = Figure(figsize=(width, height), dpi=dpi)
self.fig2 = Figure(figsize=(width, height), dpi=dpi)
self.axes1 = self.fig1.add_subplot(223)
self.axes2 = self.fig2.add_subplot(221)
[…]
self.axes2.plot([0,1])
(1)
self.axes2.set_figure(self.fig1)
(2)
self.fig1.add_axes(self.axes2)
Note that (1) and (2) are required in that order!
From the code above, if I show the fig1, then I see axes1 and axes2 on the figure…
Summary:
I created the axes2 independently of fig1
PS: I want to use my axes2 as an object to sent to my Qt widget (a blank figure/canvas)
–
Emmanuel
From: Eric Firing <efiring@…202…>
To:
matplotlib-users@lists.sourceforge.net
Sent: Sunday, April 1, 2012 1:36 PM
Subject: Re: [Matplotlib-users] assigning a plot object to a figure
On 03/30/2012 01:23 PM, Emmanuel Mayssat wrote:
Hello all,
I can create a figure
and get the corresponding axes/plots
for examples:
1/
fig = Figure(figsize=(width, height), dpi=dpi)
ax = gif.add_subplot(111)
2/
fig, ax = plt.subplots()
but I would like to create my plot independently from the figure and
assign it to a figure
I code I would like to do something like this
my_ax = Axes(…)
my_ax.plot(x,y)
ax = my_ax
or
my_ax.set_figure(fig)
How can I create the axes/plot object independently from the figure?
As far as I can see, you can’t. The figure is deeply embedded in the
Axes object; that’s why the figure is an argument in Axes.init().
Better yet, how can I assign an axe to a
figure?
I don’t think you can. It looks to me like Axes.set_figure should be
clearly marked as a private method, not to be used except in
Axes.init().
Short of a major refactoring of mpl, I think you will need to find some
other way of accomplishing your ultimate aim.
Eric
–
Emmanuel
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
Matplotlib-users mailing list
Matplotlib-users@…1753…forge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
Matplotlib-users mailing list
Matplotlib-users@…1064…t
https://lists.sourceforge.net/lists/listinfo/matplotlib-users