Creating a Barplot using pyqt

2024/10/8 8:22:05

I need plotting an animated bar chart with pyqtgraph. With animate i mean a chart, which updates his values given by a serial port. For now, a not-animated plot will be enough. I would like to implement a plot which looks something like this:

horizontal barplot

My input data is given in a dict which looks like this: (Key=Timestamp, Value=Event)

{1604496095: 0, 1604496096: 4, 1604496097: 6, 1604496098: 8, 1604496099: 9, 1604496100: 7, 1604496101: 8 ... }

Unfortunately I can't offer a lot of code as I have not been able to create a diagram similar to the one in the picture. So far I only have the corresponding window

Thats my window for now:

from PyQt5 import QtGui, QtWidgets, QtCore
import pyqtgraph as pg
import sysclass Plotter(QtWidgets.QMainWindow):def __init__(self, *args):QtWidgets.QMainWindow.__init__(self, *args)self.setWindowTitle("Test-Monitor")self.setMinimumSize(QtCore.QSize(800, 400))self.setupUI()def setupUI(self):self.plot = pg.PlotWidget()self.plot.showGrid(x=True, y=True)self.plot.setLabel('left', 'Event')self.plot.setLabel('bottom', 'Time')self.setCentralWidget(self.plot)app = QtWidgets.QApplication(sys.argv)
plotter = Plotter()
plotter.show()
app.exec_()

I would appreciate a code example that uses pyqtgraph that comes close to the picture.

Answer

You can use the BarGraphItem, and add all "bars" using arrays of values (or add individual BarGraphItems, if you prefer):

    def buildData(self, data):stamps = sorted(data.keys())zero = min(stamps)x0 = []y0 = []width = []brushes = []for i, stamp in enumerate(stamps):try:nextStamp = stamps[i + 1]except:nextStamp = stamp + 1x0.append(stamp - zero)y0.append(data[stamp])width.append(nextStamp - stamp)brushes.append(QtGui.QColor(QtCore.Qt.GlobalColor(data[stamp])))barItem = pg.BarGraphItem(x0=x0, y0=y0, width=width, height=1, brushes=brushes)self.plot.addItem(barItem)

Note that the brush colors are chosen using Qt.GlobalColor just for the purpose of this example, you should probably use a dictionary or a function that returns a color based on the value.

https://en.xdnf.cn/q/118719.html

Related Q&A

Stop Button in Tkinter

Im trying to have a turtle animation start with a button and stop with a button. Its very easy to start with a button but I cant seem to be able to figure out a stop button? Heres my code so far: imp…

binascii.Error: Incorrect padding How to decode the end with /

I received a string encoded with base64, I am using python to decode it, but decoding failed, I found that the string is followed by / ends, I dont know how to decode it, I havent found the answer, who…

How to match words in a list with user input in python?

I am working on program which takes user input and replaces the words in a list with x. eg is the word is sucks and user input is "this word is sucks". the output should be "this word i…

How to plot a ROC curve using dataframe converted from CSV file

I was trying to plot a ROC curve by using the documentation provided by sklearn. My data is in a CSV file, and it looks like this.It has two classes Goodand Badscreenshot of my CSV fileAnd my code look…

SyntaxError: Non-ASCII character. Python

Could somebody tell me which character is a non-ASCII character in the following:Columns(str) – comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible column…

A pseudocode algorithm for integer addition based on binary operation

I have tried for ages to come up with a solution but just cant get my head around it.It needs to be based on two integers on the use of standard logical operations which have direct hardware implementa…

How to efficiently split overlapping ranges?

I am looking for an efficient method to split overlapping ranges, I have read many similar questions but none of them solves my problem. The problem is simple, given a list of triplets, the first two e…

pass 2D array to linear regression (sklearn)

I want to pass 2D array to linear regression: x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0,…

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. Im running Python 3.3, and its finding the problem at the last line. How do I fix this? (A…

Pyinstaller subprocess.check_output error

Ive bundled my app with pyinstaller to 2 *.exegui_app.exe (onefile) config.ini \libs (onedir)winservice.exe+ all DLLs and libsWhen I manually install service with command winservice.exe install everyth…