Python 3.6.3:PyQt5 Matplotlib bar x Area max。 10项

时间:2017-10-26 08:34:24

标签: python matplotlib dynamic bar-chart pyqt5

我正在尝试使用matplotlib和PyQt5创建动态条形图。问题是x轴的最大元素是有限的。它可以自动调整9以下的所有条形。超过9条,第一条被覆盖。我使用matplotlib的GUI,将plot命令更改为bar并删除了不必要的代码。 http://www.boxcontrol.net/embedding-matplotlib-plot-on-pyqt5-gui.html

import sys
import random
import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

bereiche = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"]

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) useing self.axes.cla() instead

        self.compute_initial_figure()

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

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

    def compute_initial_figure(self):
        pass

class MyDynamicMplCanvas(MyMplCanvas):
    """A canvas that updates itself every second with a new plot."""
    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update_figure)
        timer.start(1000)

    def compute_initial_figure(self):
        #self.axes.set_xlim(3)
        self.axes.bar(["a", "b", "c"], [1, 2, 3], color='r')

    def update_figure(self):
        # Build a list of 11 random integers between 0 and 10 (both inclusive)
        l = [random.randint(0, 10) for i in range(len(bereiche))]

        self.axes.cla()
        #self.axes.set_xlim(11)
        self.axes.bar(bereiche, l, color='r')
        self.draw()

class ApplicationWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        self.main_widget = QWidget(self)

        l = QVBoxLayout(self.main_widget)
        dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(dc)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    aw = ApplicationWindow()
    aw.setWindowTitle("PyQt5 Matplot Example")
    aw.show()
    #sys.exit(qApp.exec_())
    app.exec_()

每次图表更新时我都用call cla替换hold,并尝试设置xlim但它不起作用。 另一个问题:compute_initial_figure函数是否必要?我已经停用它并且没有任何区别。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:在循环中生成条形

不需要函数compute_initial_figure(图表将自动调整)

def update_figure(self):
    # Build a list of 11 random integers between 0 and 10 (both inclusive)
    l = [random.randint(0, 10) for i in range(len(bereiche))]
    self.axes.cla()
    i = 0
    for bereich in range(len(bereiche)):
        self.axes.bar(bereich, l[i], color='r')
        i = i + 1
    self.axes.set_xlim(-0.5, len(bereiche)-0.5)
    self.axes.set_xticks(range(len(bereiche)))
    self.axes.set_xticklabels(bereiche)
    self.draw()

首先,它将为bereiche中的每个元素设置xticks(int),然后用set_xticklabels(bereiche)覆盖它们。不一定需要xlim。

我无法弄清楚它为什么使用循环而不是像一行一样:

self.axes.bar(bereiche, l, color='r')