PyQt5 DonutChart Example

PyQtChart How to Create DonutChart in PyQt5

In this PyQtChart article iam going to show you How to Create DonutChart in PyQt5. we are going to use PyQtChart library for this article, 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,  The commercial version of PyQtChart is bundled with the commercial version of PyQt. also you can check my older articles in PyQtChart in the below links.

 

1:  PyQtChart How to Create BarChart in PyQt5

2:  PyQtChart How to Create LineChart in PyQt5

3: PyQtChart How to Create PieChart in PyQt5

 

Also you can check PyQt5 Complete Tutorials 

 

 

What is DonutChart ?

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 the proportions between slices.

 

 

So now this is the complete code for PyQtChart How to Create DonutChart in PyQt5

 

 

 

So in the above code first of all we have imported our required classes form QtChart and PyQt5, QChart and QChartView are both essential for working of Charts in PyQt5, also we have imported QPieSeries and QPieSlice. after that we have created our Window class that extends from QMainWindow, and we have added our window title and window geometry in this class also we have called our create_donutchart() method.

 

 

This is the code for creating donutchart.

OK, in the above 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.

 

 

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.

 

 

This is for creating of theme for our donutchart.

 

 

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

 

 

Also we need to make chartview as centeral widget of our window, if we don’t do this, there will not be any chart in the window.

 

 

Also every PyQt5 application must create an application object. The sys.argv parameter is a list of arguments from a command line. Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit() method or the main widget is destroyed. The sys.exit() method ensures a clean exit. The environment will be informed how the application ended.

 

 

 

Run the complete code and this will be the result

PyQtChart How to Create DonutChart in PyQt5
PyQtChart How to Create DonutChart in PyQt5

 

 

 

Also you can watch the complete video for this article

 

(Visited 800 times, 4 visits today)
Share via
Copy link
Powered by Social Snap