Matplotlib Pie Chart
We can use the pie()
method from pyplot to create a pie chart.
The syntax for the pie()
method is as follows:
matplotlib.pyplot.pie(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)[source]
Parameter Descriptions:
x: Array of floats, representing the area of each sector.
explode: Array, indicating the separation between sectors, default is 0.
labels: List, labels for each sector, default is None.
colors: Array, colors for each sector, default is None.
autopct: Format for displaying percentages inside the pie chart, %d%% for integer percentage, %0.1f for one decimal place, %0.1f%% for one decimal place percentage, %0.2f%% for two decimal place percentage.
labeldistance: The drawing position of the label marker, relative to the radius, default is 1.1, if <1 it is drawn inside the pie chart.
pctdistance: Similar to labeldistance, specifies the scale for the position of autopct, default is 0.6.
shadow: Boolean, True or False, sets the shadow for the pie chart, default is False, no shadow.
radius: Sets the radius of the pie chart, default is 1.
startangle: The starting angle for drawing the pie chart, default is counterclockwise from the positive x-axis, if set to 90, it starts from the positive y-axis.
counterclock: Boolean, sets the direction of the pointer, default is True, counterclockwise, False is clockwise.
wedgeprops: Dictionary type, default is None. Dictionary parameters passed to the wedge object to draw a pie chart. For example: wedgeprops={'linewidth': 5}
sets the wedge line width to 5.
textprops: Dictionary type, default is None. Dictionary parameters passed to the text object, used to set the format of labels and percentage text.
center: List of floats, default is (0, 0). Used to set the center position of the chart.
frame: Boolean, default is False. If True, draws an axis frame with the table.
rotatelabels: Boolean, default is False. If True, rotates each label to the specified angle.
Here is a simple example using pie()
to create a bar chart:
Example
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
plt.show()
The result is as follows:
To set labels and colors for each sector of the pie chart:
Example
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y,
labels=['A', 'B', 'C', 'D'], # Set pie chart labels
colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9"], # Set pie chart colors
)
plt.title("tutorialpro Pie Test") # Set title
plt.show()
The result is as follows:
To highlight the second sector and format the percentage output:
Example
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y,
labels=['A', 'B', 'C', 'D'], # Set pie chart labels
colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9"], # Set pie chart colors
explode=(0, 0.2, 0, 0), # Highlight the second sector, the larger the value, the farther from the center
autopct='%.2f%%', # Format the percentage output
)
plt.title("tutorialpro Pie Test")
plt.show()
Note: By default, the drawing of the first sector starts from the x-axis and moves counterclockwise: