PyQtChart How to Create PieChart in PyQt5

In this PyQtChart article iam going to show you How to Create PieChart 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 first and second article in PyQtChart in the below links.

 

 

Read PyQtChart More Articles 

1:  PyQtChart How to Create BarChart in PyQt5

2:  PyQtChart How to Create LineChart in PyQt5

 

 

 

Also you can check more Python GUI articles in the below links.

1: Kivy GUI Development Tutorials

2: Python TKinter GUI Development 

3: Psyide2 GUI Development 

4: wxPython GUI Development 

5: PyQt5 GUI Development Course

 

 

 

 

What are PieCharts ?

A pie chart, as its name implies, is a type of chart that looks like a pie. A pie chart presents
its data in the form of pie slices. The size of each slice of pie will be determined by the
overall percentage of its value compared to the rest of the data. Therefore, pie charts are
normally used to display fraction, ratio, percentage, or a share of a set of data:

 

 

 

 

 

So now this is the complete code for PyQtChart How to Create PieChart 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_piechart() method.

 

 

 

 

 

These are the data for our piechart or we can call it PieSeries, basically in pyqt5 it is called

PieSeries.

 

 

 

 

 

Because we are going to add slice to value of piechart, you need add these codes for

adding slice.

 

 

 

basically we have created the object of QPieSlice, and we want the slice should be in our

third value, by this reason we are using this line of code.

 

 

 

 

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.

 

 

So in the code first we have created the object of QChart, and this line of code is hiding of the

chart legend.

 

 

 

 

This is used for creating of default axes for the chart.

 

 

 

 

These codes are for creating animation for our chart with title.

 

 

 

 

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. 

 

 

 

 

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. 

 

 

 

 

 

So now run the complete code and this will be the result.

PyQtChart How to Create PieChart in PyQt5
PyQtChart How to Create PieChart in PyQt5

 

 

 

 

 

 

Also you can watch the complete video for this article.

 

Subscribe and Get Free Video Courses & Articles in your Email

 

6 thoughts on “PyQtChart How to Create PieChart in PyQt5”

  1. Picking up QtCharts skills today…Thanks for the great starting point 🙂

    Working with Python 3.6 and Qt 5.15.0…
    Some minor issues in the example code…

    1) Remove QPieSlice from the imports (not needed – see next…)
    2) This line is unnecessary in your implementation:
    slice = QPieSlice() # slice is immediately reassigned in the next line…
    The instantiation of QPieSlice occurs automagically as part of
    … series.slices()[2]
    3) “slice” is a reserved word in Python. Rename…

    Thx 🙂

  2. Hi! I need little help from you actually I need a circular progress bar in pyqt5. We can also use pie chart or something like that in background. kindly help me in this regard.

  3. I have also seen in the tutorial videos that you have added many other widgets in Qt Designer. Please help me with how to do that.

Comments are closed.

Share via
Copy link
Powered by Social Snap
×