PyQtGraph Tutorial – Drawing Graphs in Python

In this PyQtGraph Tutorial  we are going to learn Drawing Graphs in Python with PyQtGraph , we learn how you can install Pyqtgraph and how you can draw or plot different charts like scatter plot, bar graph and plotting curves.

 

 

 

 

What is PyQtGraph ?

PyQtGraph is a pure-python graphics and GUI library built on PyQt4/PyQt5/PySide/ PySide2 and numpy. It is intended for use in mathematics / scientific / engineering applications. Despite being written entirely in python, the library is very fast due to its heavy leverage of numpy for number crunching, Qt’s GraphicsView framework for 2D display, and OpenGL for 3D display.

 

 

 

Learn More on Python

 

 

Installation 

OK first of all you need to install PyQtGraph, you can use pip for the installation.

 

 

 

Drawing Line in PyQtGraph

OK now let’s draw a simple line Graphs in PyQtGraph, so this is the complete code for drawing line.

 

 

So these are the imports that you need, you can see that we have imported QApplication from PyQt5, also we need to pyqtgraph.

 

 

You need to create the object of QApplication, the sys.argv parameter is a list of arguments from a command line.

 

 

You can use plot() function from pyqtgraph for plotting our line graph.

 

 

We call app.exec_() to start up the event loop.

 

 

Run the complete code and this is the result.

PyQtGraph Tutorial - Drawing Graphs in Python
PyQtGraph Tutorial – Drawing Graphs in Python

 

 

 

PyQtGraph Plotting Scatter

OK now let’s plot scatter graph in PyQtGraph, so first of all what is scatter plot, a scatter plot is a two dimensional data visualization that uses dots to represent the values obtained for two different variables – one plotted along the x-axis and the other plotted along the y-axis. Scatter plots are used when you want to show the relationship between two variables. Scatter plots are sometimes called correlation plots because they show how two variables are correlated.

 

 

This is the complete code for drawing scatter plot.

 

 

OK for drawing scatter plot first you need to give your values, we are using random numbers from numpy library, and we give random values for the x and y positions.

 

 

After that we plot the scatter, using plot() function of pyqtgraph.

 

 

Run the complete code and this is the result.

PyQtGraph Scatter Plot
PyQtGraph Scatter Plot

 

 

 

Plotting BarGraph in PyQtGraph

Let’s plot bargraphs in pyqtgraph, first of all what is BarGraph ? A bar graph is a pictorial rendition of statistical data in which the independent variable can attain only certain discrete values. The dependent variable may be discrete or continuous. The most common form of bar graph is the vertical bar graph, also called a column graph. In a vertical bar graph, values of the independent variable are plotted along a horizontal axis from left to right.

 

 

 

This is the code for creating bargraph. 

 

 

 

In here we have created the object of QApplication and also Pyqtgraph.

 

 

These are the data for BarGraph.

 

 

So in here we are going to create our BarGraph items.

 

 

Also in here we are going to add our BarGraph items to the window.

 

 

This is for handling mouse clicks for BarGraph items.

 

 

 

Run the complete code and this is the result.

PyQtGraph Drawing Bar Graph
PyQtGraph Drawing Bar Graph

 

 

 

Plotting Curves in PyQtGraph

In this section we want to draw curves in pyqtgraph, so this is the complete code.

 

 

This is the plotting data, we are using numpy for creating the data in a range.

 

 

In here we have given a title for the graph.

 

 

 

Run the code and this is the result.

PyQtGraph Curves
PyQtGraph Curves

 

 

Creating Legends in PyQtGraph

In this section we want to create legends, so first of all what are legends ? Legends are a useful way to label data series plotted on a graph. now this is the code for this example.

 

 

 

In this code we have created pg.plot(), also we have given a title to our graph, after that we added the legend to our graph.

 

 

And these are our data that we want to plot.

 

 

 

Run the code and this is the result.

PyQtGraph Tutorial - Drawing Graphs in Python
PyQtGraph Tutorial – Drawing Graphs in Python

 

 

 

Leave a Comment