can you create a barplot using pyqt

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top