Issue
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:
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 sys
class 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.
Solution
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 + 1
x0.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.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.