SoFunction
Updated on 2024-10-29

Matplotlib Pie Charting for Python Data Analysis

Preface:

Pie charts are used to present the size of the items in a data series as a percentage of the sum of the items, and Matplotlib provides the()method of plotting bar graphs.The syntax is formatted as follows:

(x, explode=None, labels=None, colors=None, autopct=None,
        pctdistance=0.6, shadow=False, labeldistance=1.1,
        startangle=0, radius=1, counterclock=True, wedgeprops=None,
        textprops=None, center=(0, 0), frame=False,
        rotatelabels=False, *, normalize=None, data=None)

Commonly used parameters and descriptions are listed below:

  • x: array type, the data for plotting the pie chart, represents the size of the sector corresponding to the array element
  • explode: indicates the interval between sectors.
  • labels: the labels of each sector, receive list type
  • colors: the color of each sector, the default is set automatically according to the color cycle.
  • autopct: Indicates the format of displaying the percentage of each sector within the pie chart, which can be displayed in a formatted way, e.g. set to%d%%Indicates an integer percentage, set to%0.2findicates the retention of two decimal places.%0.2f%%Percentage expressing one decimal place
  • pctdistance: indicates the percentage label relative to the radius of the pie chart, the default is 0.6, that is, within the pie chart, such as the number set to >1 indicates that the pie chart outside the display
  • labeldistance: indicates the ratio of labels labels relative to the radius, default value is 1.1, means outside the pie chart
  • radius: the size of the radius of the pie chart, default is 1.
  • shadow: indicates whether to add a shadow effect to the pie chart, default is False.

Of course, there are other parameters that are not commonly used, so I won't list them here.

For example, plotting a pie chart of the percentage of popularity of each city:

import  as plt
x = [20, 30, 15, 35]
# Pie chart labels
labels = ["tianjin","shanghai","jinan","beijing"]
(x,labels=labels,autopct='%.2f%%')
()

The resultant output is as follows:

This article on Python data analysis of Matplotlib pie charts to this article, more related to Matplotlib pie charts, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!