[PyQt] Display a multi-axis graph with QtChart

Introduction

Until now, pyqtgraph was used to display and create graphs in Python. However, it seems that it has not been updated in the last 3 years, so I want to switch to the one supported by Qt anyway, so QtChart I tried using /qtcharts-index.html).

environment

Installation

$ pip install PyQt5

$ pip install PyQtChart

Created sample

qchart_sample.png

I created the time on the X-axis and the current / voltage on the Y-axis.

The source is as follows.

qchart_example.py


import random
import sys

from PyQt5.QtChart import (QChart,
                           QChartView,
                           QLineSeries,
                           QValueAxis,
                           QDateTimeAxis)
from PyQt5.QtCore import (QDateTime,
                          Qt)
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication


app = QApplication(sys.argv)

cur_series = QLineSeries()
vol_series = QLineSeries()
now = QDateTime.currentDateTime()

#Create appropriate data
for i in range(100):
    cur = 5 * random.random()
    vol = 20 * random.random()
    time = now.addSecs(i).toMSecsSinceEpoch()  #Processing to append to QLineSeries
    cur_series.append(time, cur)
    vol_series.append(time, vol)

chart = QChart()
chart.legend().hide()
chart.addSeries(cur_series)
chart.addSeries(vol_series)

#Create X axis
time_axis_x = QDateTimeAxis()
time_axis_x.setFormat("hh:mm:ss")
chart.addAxis(time_axis_x, Qt.AlignBottom)
cur_series.attachAxis(time_axis_x)
vol_series.attachAxis(time_axis_x)

#Create Y1 axis
cur_axis_y = QValueAxis()
cur_axis_y.setTitleText("Current[A]")
cur_axis_y.setLinePenColor(cur_series.pen().color())  #Make the axis and chart colors the same
cur_axis_y.setRange(0, 5)
chart.addAxis(cur_axis_y, Qt.AlignLeft)
cur_series.attachAxis(cur_axis_y)

#Create Y2 axis
vol_axis_y = QValueAxis()
vol_axis_y.setTitleText("Voltage[V]")
vol_axis_y.setLinePenColor(vol_series.pen().color())  #Make the axis and chart colors the same
vol_axis_y.setRange(0, 20)
chart.addAxis(vol_axis_y, Qt.AlignRight)
vol_series.attachAxis(vol_axis_y)

cur_vol_chart_view = QChartView()
cur_vol_chart_view.setChart(chart)
cur_vol_chart_view.setRenderHint(QPainter.Antialiasing)  #To display the chart smoothly
cur_vol_chart_view.show()

sys.exit(app.exec_())

Reference URL

Recommended Posts

[PyQt] Display a multi-axis graph with QtChart
Draw a graph with NetworkX
Draw a graph with networkx
Draw a graph with Julia + PyQtGraph (2)
Draw a loose graph with matplotlib
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with pandas + XlsxWriter
Let's make a graph with python! !!
Make a nice graph with plotly
Draw a graph with PySimple GUI
Sample program to display video with PyQt
Draw a graph with PyQtGraph Part 1-Drawing
Create a graph with borders removed with matplotlib
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Draw a flat surface with a matplotlib 3d graph
Draw a graph with Japanese labels in Jupyter
How to draw a 2-axis graph with pyplot
I made a GUI application with Python + PyQt5
A memo that made a graph animated with plotly
Draw a graph with PyQtGraph Part 3-PlotWidget settings
Draw a graph by processing with Pandas groupby
[Python] Draw a directed graph with Dash Cytoscape
Simply display a line graph on Jupyter Notebook
Draw a graph with PyQtGraph Part 4-PlotItem settings
Draw a graph with matplotlib from a csv file
Draw a graph with PyQtGraph Part 6-Displaying a legend
I made a random number graph with Numpy
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
[Python] How to draw a line graph with Matplotlib
Visualize railway line data as a graph with Cytoscape 2
Create a 3D model viewer with PyQt5 and PyQtGraph
Display / update the graph according to the input with PySimpleGui
Draw a graph with PyQtGraph Part 2--Detailed plot settings
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
A4 size with python-pptx
Band graph with matplotlib
3D display with plotly
Taskbar display with tqdm
Decorate with a decorator
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Try to bring up a subwindow with PyQt5 and Python
How to display a list of installable versions with pyenv
[Visualization] I want to draw a beautiful graph with Plotly
Display a web page with FastAPI + uvicorn + Nginx (SSL / HTTPS)