Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hardware and software Environment
- Windows 10 64bit
- Anaconda3 with python 3.8
- PyCharm 2021.1
- PyQt 5.15
Introduction to the
Pie charts are the most common form of data visualization, as defined in Wikipedia
A pie chart, or pie chart, is a circular statistical chart divided into sectors to describe relative relationships among quantities, frequencies, or percentages. In pie charts, the size of each sector’s arc length (as well as its central Angle and area) is proportional to the amount it represents. Together, these sectors make a perfect circle. As the name suggests, the sectors form a cut pie pattern.
The chart below is a typical pie chart
PyQtChart
This article uses the PyQtChart library to draw pie charts. This library, which we need to install separately, is not in PyQt5, although they look similar
pip install PyQtChart
Copy the code
Code practice
The necessary comments are already written in the line of code
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice from PyQt5.QtGui import QPainter, QPen from PyQt5.QtCore import Qt class Window(QMainWindow): def __init__(self): Super ().__init__() self.setwindowTitle ("PyQt5 pie chart ") # display position self.setgeometry (100, 100, 800, 600) self.create_piechart() self.show() def create_piechart(self): Series = QPieSeries() # append = QPieSeries() Append ("Java", 7) series. Append ("C", 6) series. Append ("C++", 5) series.append("PHP", 4) series.append("Swift", 3) slice = QPieSlice() With C++ items, the indexes are 3 slice = screen.slices ()[0] # highlighted, SetLabelVisible (True) Slice.setPen (QPen(qt.red, 2)) Slice.setBrush (qt.red) # create a QChart instance, which is the PyQt5 class chart = QChart() # QLegend class is the display chart legend, Chart.legend ().hide() chart.addSeries(series) chart.CreateDefaulTaxes ( Chart. SetAnimationOptions (QChart. SeriesAnimations) # set title chart. The setTitle (" pie chart example ") chart. The legend (). The setVisible (True) # alignment Chart.legend ().setalignment (qt.alignbottom) # create ChartView, Chartview = QChartView(chart) ChartView. SetRenderHint (QPainter.Antialiasing) Self. setCentralWidget(ChartView) App = QApplication(sys.argv) window = Window() sys.exit(App.exec_())Copy the code
Finally, the code execution looks like this
The resources
- Pypi.org/project/PyQ…
Download the source code
Github.com/xugaoxiang/…
PyQt5 series tutorials
For more PyQt5 tutorials, please go
Xugaoxiang.com/category/py…