📅 2013-Oct-15 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ matplotlib, pie chart ⬩ 📚 Archive
Creating a pie chart is easy using Matplotlib. A simple pie chart can be created by passing the values representing each of the slices of the pie to the matplotlib.pyplot.pie
method. For example:
#!/usr/bin/env python
import matplotlib.pyplot as pyplot
= [1, 3, 6]
x_list
pyplot.pie(
x_list
) pyplot.show()
Typically, you want some additions to the simple pie chart:
The pie chart is oval by default. To make it a circle, call matplotlib.pyplot.axis("equal")
To show labels of each pie slice, pass them as a list of strings to the labels
parameter of the matplotlib.pyplot.pie
method.
To show the percentage of each pie slice, pass an output format to the autopct
parameter of the matplotlib.pyplot.pie
method.
To display a title for the pie chart, call the matplotlib.pyplot.title
method.
The pie chart shown above was created by these simple additions. The code is below:
#!/usr/bin/env python
import matplotlib.pyplot as pyplot
= [1, 3, 6]
x_list = ["bells", "whistles", "pasta"]
label_list
"equal")
pyplot.axis(
pyplot.pie(
x_list,=label_list,
labels="%1.1f%%"
autopct
)"Pastafarianism expenses")
pyplot.title( pyplot.show()
Tried with: Matplotlib 1.3.0 and Ubuntu 12.04 LTS