PyQt5 Tutorials
About Lesson

In this Python PyQt5 Tutorial we are going to start working with Python PyQt5 Charts, we want to draw different charts like barchart, pie chart, line chart and donut chart in Python PyQt5, for creating charts in Python PyQt5 we need to use PyQtChart library in pyqt5, so PyQtChart is a set of Python bindings for The Qt Company’s Qt Charts library. The bindings sit on top of PyQt5 and are implemented as a single module. 

 

 

First of all you need to install pyqtchart, you can use pip for the installation.

 

 

 

1: Creating BarChart in PyQt5

Now we want to create barchart in pyqt5, this is the complete source code.

 

 

In the above code first you need to create some barsets, you can use QBarSet class for this. 

 

 

After that you need to insert some data to the barset.

 

 

Because we want to create percent barset, so for this you need to create QPercentBarSeries. 

 

 

For drawing of every kind charts, you need to create the object of QChart and QChartView, first we want to create our QChart. also we have added some features like title and animation to the chart.

 

 

Also we need to create axis for the chart.

 

 

Now you need to create QChartView and add the chart in the chartview.

 

 

At the end you need to create QVBoxLayout and add the chartview in the vertical box layout.

 

 

 

Run the complete code and this is the result.

PyQt5 Tutorial - Working with PyQtChart
PyQt5 Tutorial – Working with PyQtChart

 

 

2: Creating PieChart in PyQt5

Let’s create piechart, this is the complete code for creating piechart.

 

 

You can use QPieSeries for creating the piechart in pyqt5.

 

 

These are some data that we have added to the series.

 

 

If you want to add slice to piechart than you can use this code.

Also you need to create QChart and QChartView and add the chartview in the VBoxLayout.

 

 

 

Run the complete code and this is the result.

PyQt5 Tutorial - Working with PyQtChart
PyQt5 Tutorial – Working with PyQtChart

 

 

3: Creating LineChart in PyQt5

Now we want to create linechart in pyqt5, so linecharts are typically presented as a series of points/markers that are connected by lines. In a line chart, the points are connected by straight lines to show the changes of the variables over a period of time. this is the complete code.

 

 

 

These are the data for our QLineSeries ,  also we have created the object of QLineSeries in here.

 

 

OK now i have said that when you want to create some kind of charts in PyQtChart, creating of QChart and QChartView are important for every kind of charts. and now first we create our QChart object and we add our series to QChart object and we set a title with animation for our QChart.

 

 

And at the end we create our QChartView and we add our QChart object in our ChartView.

 

 

 

Run the complete code and this is the result.

Working with PyQtChart
Working with PyQtChart

 

 

 

4: Creating DonutChart in PyQt5

Let’s create DonutChart in PyQt5, a donut chart is a Pie Chart with an area of the center cut out. Pie Charts are sometimes criticized for focusing readers on the proportional areas of the slices to one another and to the chart as a whole. This makes it tricky to see the differences between slices, especially when you try to compare multiple Pie Charts together. A Donut Chart somewhat remedies this problem by de-emphasizing the use of the area. Instead, readers focus more on reading the length of the arcs, rather than comparing. so this is the complete code for creating donutchart.

 

 

 

This is the code for creating donutchart. in this code first of all we have created the object for QPieSeries, in the introduction of donutchart we have said, that it is a type of PieChart. and we are adding some data to the QPieSeries with some functionality of DonutChart.

 

 

 

Run the complete code and this is the result.

PyQt5 DonutChart
PyQt5 DonutChart