简体   繁体   English

PYQT和嵌入matplotlib:该图未显示

[英]PYQT and embedding matplotlib: Graph not showing

Hello im trying to add a custom graph to the pyqt interface I have. 您好,我试图向我的pyqt接口添加自定义图形。 Its not showing any data, but the placeholder matplotlib graph is showing. 它没有显示任何数据,但是显示了占位符matplotlib图。 Any help?! 有帮助吗? Also If i plot just the graph data without putting it into PYQT, it shows up. 另外,如果我仅绘制图形数据而不将其放入PYQT,则会显示出来。 Thanks! 谢谢!

class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
    fig = Figure(figsize=(width, height), dpi=dpi)
    #self.axes = fig.add_subplot(111)
    # We want the axes cleared every time plot() is called
    self.axes.hold(False)

    self.compute_initial_figure()
    FigureCanvas.__init__(self, fig)
    self.setParent(parent)

    FigureCanvas.setSizePolicy(self,
                               QtGui.QSizePolicy.Expanding,
                               QtGui.QSizePolicy.Expanding)
    FigureCanvas.updateGeometry(self)

def compute_initial_figure(self):
    pass


class MyStaticMplCanvas(MyMplCanvas):

def mystatic(self, parent=None):
    super(MyStaticMplCanvas, self).__init__(parent)

    rate,data = read('test.wav') # reading
    subplot(411)
    self.plot(range(len(data)),data)
    subplot(412)
    self.specgram(data, NFFT=128, noverlap=0) # small window
    subplot(413)
    self.specgram(data, NFFT=512, noverlap=0) 
    subplot(414)
    self.specgram(data, NFFT=1024, noverlap=0) # big window

    self.show()


class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        l = QtGui.QVBoxLayout(self.main_widget)
        sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)

        l.addWidget(sc)

In the code sample you provided, there is no call to the mystatic method that you use to plot your data. 在您提供的代码示例中,没有调用用于绘制数据的mystatic方法。 Therefore, nothing is plotted on your figure. 因此,您的图形上没有任何内容。

Moreover, It seems you are using the pyplot interface for plotting your data by calling directly subplot and plot for example in mystatic . 而且,看来你正在使用的pyplot接口调用直接绘制你的数据subplot ,并plot例如在mystatic。 When embedding a mpl figure in an application, it is recommended from the mpl documentation to stick to the object oriented API. 在应用程序中嵌入mpl图形时,建议从mpl文档中使用面向对象的API。

I've produced a minimal working example from the code you've provided that follows the guidelines provided in the aforementioned documentation. 我根据您提供的代码提供了一个最小的工作示例,该示例遵循上述文档中提供的准则。 Hope it helps. 希望能帮助到你。

from PyQt4 import QtGui
import sys
import numpy as np
import matplotlib as mpl    
mpl.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class MyMplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):        
        fig = mpl.figure.Figure(figsize=(width, height), dpi=dpi)   
        fig.set_tight_layout('tight')
        super(MyMplCanvas, self).__init__(fig)

class MyStaticMplCanvas(MyMplCanvas):

    def mystatic(self, parent=None):

        x, y = np.random.rand(50), np.random.rand(50)
        ax1 = self.figure.add_subplot(411)
        ax1.plot(x,y, '.')
        ax2 = self.figure.add_subplot(412)
        ax2.plot(x,y, '.')
        ax3 = self.figure.add_subplot(413)
        ax3.plot(x,y, '.')
        ax4 = self.figure.add_subplot(414)
        ax4.plot(x,y, '.')

if __name__ == '__main__' :

    app = QtGui.QApplication(sys.argv)

    w = MyStaticMplCanvas(width=5, height=6, dpi=100)
    w.mystatic()
    w.show()

    sys.exit(app.exec_()) 

which results in: 结果是:

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM