How to plot a pie in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Plotting is one of the fundamental tool of the modern Data Scientist.
Simple plots are made easy using the .plot() from the Pandas library
You will also need to use the matplotlib library
Installing Pandas and Matplotlib
pip install pandas matplotlibpip3 install pandas matplotlibUsing Pandas
Pandas do have a little charting library in his toolbox. But will require matplotlib if you want to show it if you are running it from a script.
Here is the simplest example :
# We import the pandas along with the matplotlib library
import pandas as pd
import matplotlib.pyplot as plt # (Optional) if you run it in Jupyter
# We create our dataframe
df = pd.DataFrame(index=["USA", "France", "Germany", "Canada", "Italy"], data={"y1" : range(1,6)})
# We plot the dataframe
df.plot(kind="pie", y="y1")
# We need to show the plot.
plt.show() # (Optional) if you run it in JupyterYou can pass quite a lot of paremeters to the .plot() method.
Such as
- A title as title="My amazing title"
- the type of chart here kind=pie default is line
- etc...
Like so :
# We import the pandas along with the matplotlib library
import pandas as pd
import matplotlib.pyplot as plt # (Optional) if you run it in Jupyter
# We create our dataframe
df = pd.DataFrame(index=["USA", "France", "Germany", "Canada", "Italy"], data={"y1" : range(1,6)})
# We plot the dataframe
df.plot(kind="pie", y="y1", title="Example of pie plot")
# We need to show the plot.
plt.show() # (Optional) if you run it in JupyterHere you go ! You know how to make a basic pie plot in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚