To make a pie chart in Python you need four things (and then another two things to display it properly):
python3.9 -m pip install matplotlib
python3.9
with whatever version of Python you haveplt
axes()
function and, in this example, assigned to the variable ax
pie()
command from the Pyplot sub-packageThe command show()
will then display the pie chart and close()
will close it afterwards (this is necessary if you want to continue plotting other charts and graphs):
import matplotlib.pyplot as plt
# Create data
data = [12, 9, 17]
# Plot
ax = plt.axes()
ax.pie(data)
plt.show()
plt.close()
Of course, this is a little empty. Fill in the chart with a title and labels:
set_title()
adds a title
fontsize
keyword argument can be used to change the font sizelabels
parameter of the pie()
function determines the labelsIn this example, the data is create as a dictionary. The values are plotted in the pie chart and the keys are used as the labels:
import matplotlib.pyplot as plt
# Create data:
# Wolverhampton Wanderers' results in the 2020–21 Premier League
results = {'Won': 12, 'Drawn': 9, 'Lost': 17}
# Plot
ax = plt.axes()
ax.pie(results.values(), labels=results.keys())
ax.set_title("Wolverhampton Wanderers' results\nin the 2020–21 Premier League", fontsize=16)
plt.show()
plt.close()
Edit the labels to add more detail. If you are using special characters like “%” you will need to convert the labels to raw strings by prepending the letter “r”:
import matplotlib.pyplot as plt
# Create data:
# Wolverhampton Wanderers' results in the 2020–21 Premier League
results = {r'Won (32%)': 12, r'Drawn (24%)': 9, r'Lost (45%)': 17}
# Plot
ax = plt.axes()
ax.pie(results.values(), labels=results.keys())
ax.set_title("Wolverhampton Wanderers' results\nin the 2020–21 Premier League", fontsize=16)
plt.show()
plt.close()
A way of doing the above without having to hardcode the values in is provided by the autopct
parameter. Set this equal to a formatting instruction such as would be used by an f-string (more about those here):
import matplotlib.pyplot as plt
# Create data:
# Wolverhampton Wanderers' results in the 2020–21 Premier League
results = {'Won': 12, 'Drawn': 9, 'Lost': 17}
# Plot
ax = plt.axes()
ax.pie(results.values(), labels=results.keys(), autopct='%1.0f%%')
ax.set_title("Wolverhampton Wanderers' results\nin the 2020–21 Premier League", fontsize=16)
plt.show()
plt.close()
colors
parameter
textprops
parameter, which takes a dictionary as its inputpatheffects
sub-module is neededpie
function (in this example, they are assigned to the variable autotexts
)set_path_effects()
method to set a path around each of themimport matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
# Create data:
# Wolverhampton Wanderers' results in the 2020–21 Premier League
results = {'Won': 12, 'Drawn': 9, 'Lost': 17}
# Custom colours
colors = ['#FDB913', '#000000', '#231F20']
# Plot
ax = plt.axes()
patches, texts, autotexts = ax.pie(
results.values(), labels=results.keys(), autopct='%1.0f%%', colors=colors, textprops={'fontsize': 12}
)
for autotext in autotexts:
autotext.set_path_effects(
[path_effects.Stroke(linewidth=2, foreground='white'), path_effects.Normal()]
)
ax.set_title("Wolverhampton Wanderers' results\nin the 2020–21 Premier League", fontsize=16)
plt.show()
plt.close()
The run commands (RCs), which are the instructions that get read first when the code is run, can be edited with Pyplot’s rc()
command. These can be used to set the figure size and quality, to use Latex for the graph’s text and to add Latex packages:
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
# Settings
x = 5 # Want figures to be A6
plt.rc('figure', figsize=[46.82 * .5**(.5 * x), 33.11 * .5**(.5 * x)])
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.rc('text.latex', preamble=r'\usepackage{textgreek}')
# Create data:
# Wolverhampton Wanderers' results in the 2020–21 Premier League
results = {'Won': 12, 'Drawn': 9, 'Lost': 17}
# Custom colours
colors = ['#FDB913', '#000000', '#231F20']
# Plot
ax = plt.axes()
patches, texts, autotexts = ax.pie(
results.values(), labels=results.keys(), autopct=r'%1.0f\%%', colors=colors, textprops={'fontsize': 14}
)
for autotext in autotexts:
autotext.set_path_effects(
[path_effects.Stroke(linewidth=1, foreground='white'), path_effects.Normal()]
)
ax.set_title("Wolverhampton Wanderers' results\nin the 2020–21 Premier League", fontsize=16)
plt.show()
plt.close()