14 Feb Matplotlib – Pie Chart
To plot a pie chart in Matplotlib, use the pie() method. The pyplot.pie() method has the following parameters:
- The 1st parameter is x, the wedge sizes are set here
- The labels parameter is the sequence of strings providing the labels for each wedge
- The third parameter is autopct, a string that labels the wedges with their numeric value. The label will be placed inside the wedge. If autopct is a format string, the label will be fmt % pct
Let us see an example wherein we will plot a pie chart for players scoring runs in a match:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import matplotlib.pyplot as plt import numpy as np # Data player = np.array(["Tim", "John", "Amit", "Jacob", "Karl", "Gary"]) score = np.array([85, 59, 99, 77, 67, 95 ]) # Plot a pie chart using the pyplot.pie() method plt.pie(score, labels = player, autopct='%1.2f%%') # Display the figure plt.show() |
Output
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments