How to do a pie plot with Matplotlib
• 1 minHere is the simplest method to do a pie plot.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(data={"country" : ["Spain", "Germany", "USA", "Spain"]})
fig, axes = plt.subplots(1,1, figsize=(8,6))
# We do a pie plot on the axes
axes.pie(x=df["country"].value_counts(),
labels=df["country"].value_counts().index)
# Fixing the layout to fit the size
fig.tight_layout()
# Showing the plot
plt.show()
Note the use of the .value_counts() method that gives the counts of unique values.
More on plots
If you want to know more about how to add labels, plot different types of plots, etc... checkout the other articles I wrote on the topic, just here :
Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.