Issue
My project has a class GUI
that uses PySide to embed Matplotlib's FigureCanvas inside one of two cells in a QGridLayout. The other class is MplGrapher
, which originally was an extension of FigureCanvas. At first, everything was working fine except the plot didn't have a Navigation Toolbar. Then I found a tutorial that explained that if you want a Navigation Toolbar with your embedded plot, you need create an instance of it first...so after this, my plot included a toolbar, but it also decreased in size and I can't get it to fill the cell of GUI's
QGridLayout. I've tried having the MplGrapher
class extend QWidget instead...I've tried creating a sub layout inside MplGrapher
that houses the plot and the toolbar...I've tried setting QSizePolicy, but nothing seems to work.
Part of what's making this so difficult for me is that I'm not sure the proper way to create a FigureCanvas, and I'm not sure the proper way to create a NavigationToolbar. There are too many different ways of extending, initializing, and setting the parent of this class, MplGrapher
, that I'm loosing track of my test cases to figure the problem out for myself. Adding to the confusion is the fact that all these different test cases only produce one of three results:
- a proper sized Plot, but without a NavigationToolbar (unable to revert back to when this happens)
- a improperly sized Plot with a NavigationToolbar pic
- the QMainWindow of the
GUI
class disappears (not crashing)
Here's some code:
#FigureCanvas to be embedded in PySide GUI
class MplGrapher(FigureCanvas):
def __init__(self,name,parent):
self.figure = Figure()
self.figure.suptitle(name)
self.parent = parent
super(FigureCanvas, self).__init__(self.figure)
self.initFigure()
def initFigure(self):
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self, self.parent)
Here's the another attempt:
# FigureCanvas to be embedded in PySide GUI
class MplGrapher(QWidget):
def __init__(self,name,parent):
self.figure = Figure()
self.figure.suptitle(name)
self.parent = parent
super(MplGrapher, self).__init__(self.parent)
self.initFigure()
def initFigure(self):
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self, self.parent)
And another:
# FigureCanvas to be embedded in PySide GUI
class MplGrapher(FigureCanvas):
def __init__(self,name,parent):
self.figure = Figure()
self.figure.suptitle(name)
self.parent = parent
super(MplGrapher, self).__init__(self.figure)
self.initFigure()
def initFigure(self):
self.setParent(self.parent)
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self, self.parent)
And another:
# FigureCanvas to be embedded in PySide GUI
class MplGrapher(FigureCanvas):
def __init__(self,name,parent=None):
super(FigureCanvas, self).__init__(Figure())
self.initFigure(name)
def initFigure(self, name):
self.layout = QGridLayout()
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred)
self.figure = Figure()
self.figure.suptitle(name)
self.canvas = FigureCanvas(self.figure)
self.canvas.setParent(self)
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
self.layout.addWidget(self.canvas,0,0)
self.layout.addWidget(self.mpl_toolbar,1,0)
Solution
Something like this should do what you want.
class MplGrapher(QtGui.QWidget):
def __init__(self,name,parent=None):
super(MplGrapher, self).__init__(parent)
self.initFigure(name)
def initFigure(self, name):
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.plt = self.figure.add_subplot(111)
self.navbar = NavigationToolbar(self.canvas, self)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.navbar)
self.layout.addWidget(self.canvas)
self.setLayout(self.layout)
Answered By - user3419537
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.