Skip to content
Related Articles

Related Articles

PyQtGraph – Bar Graph
  • Last Updated : 24 Sep, 2020
GeeksforGeeks - Summer Carnival Banner

In this article we will see how we can create bar graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and second is to provide tools to aid in rapid application development (for example, property trees such as used in Qt Designer).

A bar graph is a method for visualizing a set of data. Simple bar graphs compare data with one independent variable and can relate to a set point or range of data. Complex bar graphs compare data with two independent variables. Either type of graph can be oriented horizontally or vertically. Bar graph is created with the help of BarGraphItem class in PyQtGraph.

In order to plot the bar graph in PyQtGraph we have to do the following
1. Importing the PyQtgraph module
2. Creating a plot window
3. Create or get the plotting data i.e horizontal and vertical data
4. Create a BarGraphItem object to plot the bar graph between the data
5. Append the BarGraphItem object to the plot window

Below is the implementation




# importing pyqtgraph as pg
import pyqtgraph as pg
  
# importing QtCore and QtGui from the pyqtgraph module
from pyqtgraph.Qt import QtCore, QtGui
  
# importing numpy as np
import numpy as np
  
# define the data
title = "GeeksforGeeks PyQtGraph"
  
# y values to plot by line 1
y = [2, 8, 6, 8, 6, 11, 14, 13, 18, 19]
  
# y values to plot by line 2
y2 = [3, 1, 5, 8, 9, 11, 16, 17, 14, 16]
x = range(0, 10)
  
# create plot object
plt = pg.plot()
  
# showing x and y grids
plt.showGrid(x = True, y = True)
  
# adding legend
plt.addLegend()
  
# set properties of the label for y axis
plt.setLabel('left', 'Vertical Values', units ='y')
  
# set properties of the label for x axis
plt.setLabel('bottom', 'Horizontal Vlaues', units ='s')
  
# setting horizontal range
plt.setXRange(0, 10)
  
# setting vertical range
plt.setYRange(0, 20)
  
# setting window title
plt.setWindowTitle(title)
  
# ploting line in green color
line1 = plt.plot(x, y, pen ='g', symbol ='x', symbolPen ='g',
                           symbolBrush = 0.2, name ='green')
  
# ploting line2 with blue color
line2 = plt.plot(x, y2, pen ='b', symbol ='o', symbolPen ='b',
                             symbolBrush = 0.2, name ='blue')
  
  
  
# main method
if __name__ == '__main__':
      
    # importing system
    import sys
      
    # Start Qt event loop unless running in interactive mode or using 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :