pyqtgraph bar chart

The module pyqtgraph supports all kinds of charts and scientific graphics. Underneath, pyqtgraph uses PyQt4 / PySide and numpy. Its been tested to work on Linux, Windows, and OSX.

In this article we’ll create an example bar chart. The output will be this awesome chart:

pyqtgraph barchart

Related course:
Create Simple GUI Applications with Python and Qt

pyqtgraph barchart

We create the data to plot (x y1) first. y1 is a list of 20 floats that we create using the numpy method linspace.

Then create the barchart using the method BarGraphItem(), where the parameters are x, y, bar width and color.

# pyqtgraph examples : bar chart
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

win = pg.plot()
win.setWindowTitle('pyqtgraph BarGraphItem')

# create list of floats
y1 = np.linspace(0, 20, num=20)

# create horizontal list
x = np.arange(20)

# create bar chart
bg1 = pg.BarGraphItem(x=x, height=y1, width=0.6, brush='r')

## Start Qt event loop unless running in interactive mode or using
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
Previous Post Next Post

Cookie policy | Privacy policy | ©