Python

Tag: pyqtgraph

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
# pythonprogramminglanguage.com
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')
win.addItem(bg1)

## 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'):
QtGui.QApplication.instance().exec_()

pyqtgraph plot

A simple plot can be created with the module pyqtgraph. Mind you, it’s one of the libraries for plotting, there are others like matplotlib.

We start with importing pyqtgraph and defing the plotting data (x and y). Then we plot the data using pg.plot().

pyqtgraph plot

Related course:
Create Simple GUI Applications with Python and Qt

pyqtgraph plot

The example below creates a plot using pyqtgraph.

import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np

# define the data
theTitle = "pyqtgraph plot"
y = [2,4,6,8,10,12,14,16,18,20]
x = range(0,10)

# create plot
plt = pg.plot(x, y, title=theTitle, pen='r')
plt.showGrid(x=True,y=True)

## Start Qt event loop.
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(pg.QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()

We can set additional properties of the chart like the labels, add a legend, the colors.
The output will look like this:
pyqtgraph plot legend

In code its like this:

import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np

# define the data
theTitle = "pyqtgraph plot"
y = [2,4,6,8,10,12,14,16,18,20]
y2 = [0,1,2,4,12,14,16,17,14,22]
x = range(0,10)

# create plot
plt = pg.plot()
plt.showGrid(x=True,y=True)
plt.addLegend()

# set properties
plt.setLabel('left', 'Value', units='V')
plt.setLabel('bottom', 'Time', units='s')
plt.setXRange(0,10)
plt.setYRange(0,20)
plt.setWindowTitle('pyqtgraph plot')

# plot
c1 = plt.plot(x, y, pen='b', symbol='x', symbolPen='b', symbolBrush=0.2, name='red')
c2 = plt.plot(x, y2, pen='r', symbol='o', symbolPen='r', symbolBrush=0.2, name='blue')

## Start Qt event loop.
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(pg.QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()