简体   繁体   English

如何让 FigureCanvas 在嵌入在 pyqt GUI 中的 matplotlib 小部件中填充整个图形?

[英]How can I make the FigureCanvas fill the entire Figure in a matplotlib widget embedded in a pyqt GUI?

I have attempted to make a GUI with embedded matplotlib widget in it.我试图制作一个带有嵌入式 matplotlib 小部件的 GUI。 I just need the figure to be completely filled by the FigureCanvas, I have tried about 100 different things and nothing has changed the size of the canvas one bit.我只需要用 FigureCanvas 完全填充图形,我尝试了大约 100 种不同的东西,但没有一点改变画布的大小。 I left a few of my attempts in the code denoted by "#useless" to let you know that I tried it and it had no impact.我在用“#useless”表示的代码中留下了我的一些尝试,让您知道我尝试过它并且没有任何影响。 Please help.请帮忙。

import sys
from PyQt4 import QtGui
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class Terminal(QtGui.QDialog):
def __init__(self, parent=None):
    try:        
        super(Terminal, self).__init__(parent)

        self.resize(1000,800)
        self.figure = plt.figure(facecolor='black')
        self.canvas = FigureCanvas(self.figure)
        self.canvas.autoFillBackground()#useless
        self.myComboBox = QtGui.QComboBox()
        self.plotButton = QtGui.QPushButton('Plot')      
        self.xMplWidget = MatplotlibWidget(self.canvas)         
        self.plotButton.clicked.connect(self.plotCircles)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.myComboBox)
        layout.addWidget(self.plotButton)
        policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)#useless
        self.canvas.setSizePolicy = policy#useless
        layout.setStretchFactor(self.canvas, 1)#useless
        self.setLayout(layout)
        self.canvas.autoFillBackground()#useless
    except Exception as err:
        print("Error in Terminal.init: Other - ", err)


def createDashboard(self,oDashboard):
    print("DoStuff")

def plotCircles(self):
    print("DoStuff")        




class MatplotlibWidget(FigureCanvas):
    def __init__(self, parent=None, title='Title', xlabel='x label', ylabel='y label', dpi=100, hold=False):
    super(MatplotlibWidget, self).__init__(Figure())       
    self.setParent(parent)
    self.figure = Figure(dpi=dpi)
    self.canvas = FigureCanvas(self.figure)

    self.theplot = self.figure.add_subplot(111)
    self.theplot.set_title(title)
    self.theplot.set_xlabel(xlabel)
    self.theplot.set_ylabel(ylabel)


def plotChart(self,oOxyName):
    print("DoStuff")     


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Terminal()
    main.show()

    app.exec_()

在此处输入图片说明

I figured it out after continued studying.继续学习后我明白了。 I had made a mess early on in the project and had failed to carefully reread my code when changing the initial design.我在项目的早期就搞砸了,并且在更改初始设计时没有仔细重新阅读我的代码。 The issue was that I was creating a canvas widget and passing that widget to the MplWidget.问题是我正在创建一个画布小部件并将该小部件传递给 MplWidget。 The MplWidget correctly had its own canvas, and therefor did not need an additional one passed to it. MplWidget 正确地拥有自己的画布,因此不需要传递给它的额外画布。 Essentially I was making the MplWidget within the canvas widget, rather than the MplWidget within the main form.本质上,我是在画布小部件中制作 MplWidget,而不是在主窗体中制作 MplWidget。

Here is the corrected code with notes on the corrections.这是更正后的代码,并附有更正说明。

import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
    FigureCanvasQTAgg as FigureCanvas)

class Terminal(QtGui.QDialog):
    def __init__(self, parent=None):
        try:        
            super(Terminal, self).__init__(parent)

            self.resize(1000,800)
            self.figure = plt.figure(facecolor='black')
#This was creating a canvas which I was adding my widget to (bad idea)
#            self.canvas = FigureCanvas(self.figure)

            self.myComboBox = QtGui.QComboBox()
            self.plotButton = QtGui.QPushButton('Plot')      
            self.xMplWidget = MatplotlibWidget()         
            self.plotButton.clicked.connect(self.plotCircles)

            layout = QtGui.QVBoxLayout()
            #Needed to add my MatplotlibWidget, not a canvas widget, because the MPLWidget has its own canvas
            #layout.addWidget(self.canvas)
            layout.addWidget(self.xMplWidget) #This was added

            layout.addWidget(self.myComboBox)
            layout.addWidget(self.plotButton)
            self.setLayout(layout)
        except Exception as err:
            print("Error in Terminal.init: Other - ", err)


    def createDashboard(self,oDashboard):
        print("DoStuff")

    def plotCircles(self):
        print("DoStuff")        


class MatplotlibWidget(FigureCanvas):
    def __init__(self, parent=None, title='Title', xlabel='x label', ylabel='y label', dpi=100, hold=False):
        super(MatplotlibWidget, self).__init__(Figure())       
        self.setParent(parent)
        self.figure = Figure(dpi=dpi)
        self.canvas = FigureCanvas(self.figure)

        self.theplot = self.figure.add_subplot(111)
        self.theplot.set_title(title)
        self.theplot.set_xlabel(xlabel)
        self.theplot.set_ylabel(ylabel)


    def plotChart(self,oOxyName):
        print("DoStuff")     


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Terminal()
    main.show()

    app.exec_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyQt4如何在嵌入式Matplotlib FigureCanvas周围绘制边框 - PyQt4 How to draw border around embedded Matplotlib FigureCanvas 如何使频谱图用matplotlib填充整个图形区域? - How to make specgram fill entire figure area with matplotlib? 为什么我不能在嵌入在 pyqt5 gui 中的 matplotlib 颜色图中成功绘制感兴趣的区域? - Why can't I successfully draw the region of interest in a matplotlib color map embedded in a pyqt5 gui? 当 matplotlib.FigureCanvas 嵌入到 PyQt5 应用程序中时,Matplotlib 回调不起作用? - Matplotlib callbacks don't work when matplotlib.FigureCanvas is embedded in a PyQt5 application? 如何在matplotlib中的FigureCanvas上显示GTK小部件 - How to display a GTK-widget on top of FigureCanvas in matplotlib 如何在 matplotlib 小部件的嵌入式图形上使用跨度选择器? - How to use the Span Selector on a embedded figure of matplotlib widget? Pyqt 小部件 FigureCanvas 边框和内容共存 - Pyqt widget FigureCanvas border and content coexist 如何通过拖动使 PyQt 小部件可调整大小? - How can I make a PyQt widget resizable by dragging? 在GUI嵌入式绘图中使用matplotlib Cursor小部件 - Using matplotlib Cursor widget in GUI embedded plot 如何在 PyQt5 GUI 中制作快速的 matplotlib 实时绘图 - How to make a fast matplotlib live plot in a PyQt5 GUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM