如何在窗口中的PyQt4中绘制不同画布中的不同图形?

时间:2016-05-10 00:14:44

标签: python matplotlib plot pyqt4

我正在尝试使用python3编写一个GUI,它在4个相应的布局中绘制4个不同的图形:速度,高度,坐标和角度。现在我能够在每个相应的布局中绘制图形。但是,我不知道如何在每个图中绘制不同的函数。我用一种方法随机生成10个点来绘图。调用该方法时,它会将相同的图形绘制到每个4个画布中。

所以我的问题是**如果无论如何分别为一个图形绘制不同的函数(每个图形一个图)?**我对python3很新,并且会感谢所提供的任何帮助。

如果可能的话,我想避免在图中使用很多子图,因为每个图的布局已经存在。

当我调用生成随机点的测试方法时,这是当前GUI的样子,你可以看到 the same graph are plotted in each canvas

我还要提一下,如果它添加了任何约束,那么这段代码最终将用于绘制将使用来自另一个线程的数据进行更新的图形。

以下是代码:

    from PyQt4 import QtGui
    from .flight_dataUI import Ui_Dialog
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
    import random

    class FlightData(QtGui.QDialog, Ui_Dialog):
        def __init__(self, parent=None):
            QtGui.QDialog.__init__(self, parent)
            self.setupUi(self)

            self.figure = plt.figure() # FlightData.figure = matplotlib.pyplot.figure()
    #       Creates a figure widget self.name = FigureCanvas(self.figure)
            self.speedGraph = FigureCanvas(self.figure)
            self.heightGraph = FigureCanvas(self.figure)
            self.mapGraph = FigureCanvas(self.figure)
            self.angleGraph = FigureCanvas(self.figure)
    # -------------------------------------------------------------
            self.speedLayout.addWidget(self.speedGraph)  # insert widget "speedGraph" in speedLayout
            self.heightLayout.addWidget(self.heightGraph)  # insert widget "heightGraph" in heightLayout
            self.mapLayout.addWidget(self.mapGraph)  # insert widget "mapGraph" in mapLayout
            self.angleLayout.addWidget(self.angleGraph)  # insert widget "angleGraph" in angleLayout
            self.ax = self.figure.add_subplot(111)
            self.ax.hold(False)

            self.init_widgets()

        def init_widgets(self):
            self.analyseButton.clicked.connect(self.open_analysedata)
            self.draw_plot()

        def open_analysedata(self):
            self.done(2)  # Closes and delete Dialog window et and return the int 2 as a results in main_window.py

        def draw_plot(self):
            data = [random.random() for i in range(10)]
            self.ax.plot(data, '-*')

1 个答案:

答案 0 :(得分:0)

如果嵌入不导入pyplot,那么它的全局状态只会给您带来麻烦。您正在使用相同的图来初始化所有4个FigureCanvas个对象。你想做类似的事情:

from matplotlib.figure import Figure

class FlightData(QtGui.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)
        self.figs = {}
        self.canvas = {}
        self.axs = {}
        plot_names = ['speed', 'height', 'map', 'angle']
        for pn in plot_names:
            fig = Figure()
            self.canvas[pn] = FigureCanvas(fig)
            ax = fig.add_subplot(1, 1, 1)

            self.figs[pn] = fig
            self.axs[pn] = ax

        # -------------------------------------------------------------
        self.speedLayout.addWidget(self.canvas['speed'])
        self.heightLayout.addWidget(self.canvas['height'])
        self.mapLayout.addWidget(self.canvas['map'])
        self.angleLayout.addWidget(self.canvas['angle'])

    def draw_plot(self, target, data):
        self.axs[target].plot(data, '-*')
        self.canvas[target].draw_idle()