使用 PyQt5 嵌入动态条形图 [英] Using PyQt5 to embed a dynamical bar chart

查看:48
本文介绍了使用 PyQt5 嵌入动态条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用python编写了以下代码,以在PyQt5生成的GUI中显示条形图.

I wrote the following code in python to show a bar chart in the GUI generated by PyQt5.

import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, \
QLineEdit, QMessageBox, QInputDialog, QLabel, QHBoxLayout, QGridLayout,     QStackedLayout, QFormLayout
from  PyQt5 import QtCore, QtGui, QtWidgets
import time
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.lines import Line2D

import matplotlib.animation as animation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as     NavigationToolbar
import matplotlib.pyplot as plt
# data
import numpy as np
#import matplotlib.pyplot as plt
import pandas as pd
import os
import csv

#define  the path to data
pathToData='C:/RC/python/data/'
dataName= '31122017ARB.csv'
# import 
data=pd.read_csv(pathToData+dataName, delimiter=';',encoding='cp1252')


# percent  MW
data['MWP']=data[' MW ']/sum(data[' MW '])
#aggregate by   Best
datag=data.groupby('Best', as_index=False).agg({'MW': 'sum'})
x=["BE"+s for s in[str(s) for s in [int(x) for x in datag.iloc[:,0].tolist()]]]
y=datag.ix[:,1].tolist()




figure = plt.figure()

H = np.array([[100, 2, 39, 190], [402, 55, 369, 1023], [300, 700, 8, 412], [170, 530, 330, 1]])
Z = np.array([[3, 290, 600, 480], [1011, 230, 830, 0], [152, 750, 5, 919], [340, 7, 543, 812]])


class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init_
        self.im = None
        self.canvas = FigureCanvas(self.figure)

        self.canvas.mpl_connect('button_press_event', self.on_button_press_event)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.plot)
        self.timer.setInterval(5000)

        # Just some button connected to `plot` method
        self.button = QPushButton('Plot')
        self.button.clicked.connect(self.timer.start)
        self.button.setDefault(False)

        self.stop = QPushButton("Stop")
        self.stop.clicked.connect(self.timer.stop)
        self.stop.setDefault(False)

        self.exit = QPushButton('Exit')
        self.exit.clicked.connect(self.close)
        self.exit.setDefault(True)

         layout = QFormLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        layout.addWidget(self.stop)
        layout.addWidget(self.exit)

        self.setLayout(layout)

        self.lb = QtWidgets.QLabel(self)
        self.lb.setWindowFlags(QtCore.Qt.ToolTip)

            def plot(self):
        self.x  = ["BE"+s for s in[str(s) for s in [int(x) for x in datag.iloc[:,0].tolist()]]]
        self.y  = datag.iloc[:,1].tolist()#np.sin(self.x)

        self.setWindowTitle("Bestandseingruppierung")

    def update_figure(self):
        self.axes.bar(self.x, self.y)
        #self.y = np.roll(self.y,-1)
        #self.draw()
        self.canvas.draw()
    def on_button_press_event(self, event):
        print('button={}, x={}, y={}, xdata={}, ydata={}'
            .format(event.button, event.x, event.y, event.xdata, event.ydata))

       if self.im:
                message = str(self.im.get_cursor_data(event))
                delay = 1000
                w = self.lb.fontMetrics().width(message)
                self.lb.resize(w, self.lb.size().height())
                self.lb.setText(message)
                self.lb.move(QtGui.QCursor.pos())
                self.lb.show()
                QtCore.QTimer.singleShot(delay, self.lb.hide)




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

        main = Window()
        main.show()

        sys.exit(app.exec_())

问题是:它没有显示任何情节,即使我没有收到任何错误.我只是获得一个空的 .

The Problem is: it is not shown any plot, even I do not get any error. I just obtain an empty .

如何通过按下绘图底部来获取图表?我想,我在 def plot(self): 下做错了什么.

How can I get the chart by pressing the plot bottom? I guess, I am doing some thing wrong under def plot(self):.

只是为了澄清:我在 PyQt5 生成的 GUI 中测试了图表作为独立代码,它完全正常:

Just because of clarification: I tested the chart within a GUI generated by PyQt5 as a stand alone code, which works totally fine:

# FigureCanvas inherits QWidget
class MainWindow(FigureCanvas):
    def __init__(self, parent=None, width=4, height=3, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        self.axes.hold(False)

        super(MainWindow, self).__init__(fig)
        self.setParent(parent)

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

        timer = QTimer(self)
        timer.timeout.connect(self.update_figure)
        timer.start(50)

        self.x  = ["BE"+s for s in[str(s) for s in [int(x) for x in datag.iloc[:,0].tolist()]]]#np.arange(0, 4*np.pi, 0.1)
        self.y  = datag.iloc[:,1].tolist()#np.sin(self.x)

        self.setWindowTitle("Best")

    def update_figure(self):
        self.axes.bar(self.x, self.y)
        #self.y = np.roll(self.y,-1)
        self.draw()

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

    mainWindow.show()
sys.exit(app.exec_())

推荐答案

我找到了解决方案!

我在此 GUI 中有 3 个 boton,如代码的以下部分所示:

I have 3 botons in this GUI as shown in the following part of the code:

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init_
        self.im = None
        self.canvas = FigureCanvas(self.figure)

        self.canvas.mpl_connect('button_press_event', self.on_button_press_event)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.plot)
        self.timer.setInterval(0)

        # Just some button connected to `plot` method
        self.button = QPushButton('Plot')
        self.button.clicked.connect(self.timer.start)
        self.button.setDefault(False)

        self.stop = QPushButton("Stop")
        self.stop.clicked.connect(self.timer.stop)
        self.stop.setDefault(False)

        self.exit = QPushButton('Exit')
        self.exit.clicked.connect(self.close)
        self.exit.setDefault(True)

必须将self.timer.setInterval(0) 中的时间延迟设置为零!这是第一点.

One has to set the time delay in self.timer.setInterval(0) to Zero! That is the first point.

def plot(self) 部分应该修改如下:

def plot(self):
        data=x
        self.setWindowTitle("Bestandseingruppierung")
        # instead of ax.hold(False)
        self.figure.clear()

        # create an axis
        ax = self.figure.add_subplot(111)
        #fig.autofmt_xdate()
        # discards the old graph
        ax.hold(False) # deprecated, see above

        # plot data
        ax.axes.bar(data,y)
        self.canvas.draw()

这篇关于使用 PyQt5 嵌入动态条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆