简体   繁体   中英

Smooth matplotlib plot with lot of data (PyQt5)

I'm trying to view some data on a matplotlib plot, the data is from a generator that I have been monitoring, but the graph line looks horrible as I'm using a lot of points (800 per day, and I want to display an entire month), I'm pretty sure there is a way to make it look smoother or at least to automatically filter some points, but I'm not sure how to apply it to a PyQT5 Matplotlib widget. I'm using PyQt5 because I want to make the graph interactive (active another lines to display with buttons).

I'm using a QMainWindow with an emphy matplotlib canvas:

    from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1024, 768)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.mplwidget = MatplotlibWidget(self.centralwidget)
        self.mplwidget.setObjectName("mplwidget")
        self.gridLayout.addWidget(self.mplwidget, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Fuel Consumption Chart"))

from matplotlibwidget import MatplotlibWidget

Then, I use another file to use that UI and customize my plot:

import FuelUI
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
import datetime as dt
import matplotlib.dates as md
import json
import os
from FuelData import getData
from TempData import getDataTemp

class FuelChart(QMainWindow, FuelUI.Ui_MainWindow):
    def __init__(self, parent = None):
        super(FuelChart, self).__init__(parent)
        self.setupUi(self)

        self.mplwidget.axes.set_xlabel('Time (days)')
        self.mplwidget.axes.set_ylabel('Energy (BTU/h)')
        self.mplwidget.axes.set_title('Fuel Comsumption Chart')
        self.mplwidget.axes.grid(True)
        for tick in self.mplwidget.axes.get_xticklabels():
            tick.set_rotation(90)
        x, y = getData()
        xt, yt = getDataTemp()
        self.mplwidget.axes.plot(x, y, label ='Energy Consumption')
        self.mplwidget.axes.plot(xt, yt, label = 'Hot Water energy production')
        self.mplwidget.axes.legend()
        xformatter = md.DateFormatter('%d')
        self.mplwidget.axes.xaxis.set_major_formatter(xformatter)
        #self.mplwidget.figure.subplots_adjust(bottom=0.21)
        self.mplwidget.show()

if __name__ == "__main__":
    QApplication.setStyle("cleanlooks")
    app = QApplication(sys.argv)
    fuelchart = FuelChart()
    fuelchart.showFullScreen()
    sys.exit(app.exec_())

The getData() and getTempData() functions are python libraries that read a json with all the data gathered. If I execute the main script what I obtain is the following:

图表 As you can see, matplotlib is trying to show every point (which is good in most of cases), but as I have a lot of points, it looks pretty tight so it start to look horrible.

There is a way to make it look better?, any help or hint?

Don't loop. Use ax.xtick(rotation=90) for a vectorized rotation adjustment.

Store a handle to the axis (you have already done this) and use axis.set_xdata with axis.set_ydata before calling figure.draw(). Matplotlib can update a few thousand points well in excess of 60FPS when used with Qt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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