Integrations
Pie Chart

Matplotlib Pie Chart

Introduction

Matplotlib is a library for making charts, which works very well with Jupyter Notebook. One of them is called a Pie Chart. We've got a few examples ready to show you, so you can see what they look like and how they work:

  • simple pie chart
  • pie chart with numpy data
  • pie chart with pandas data
  • an interactive pie chart

If you need any information about Matplotlib check their docs: Matplotlib Docs (opens in a new tab).

All of code examples are availabe as Jupyter Notebooks in our GitHub repositiory:

Pie Chart

Example with using only matplotlib package:

# import packages
import matplotlib.pyplot as plt
 
# create data
x = [15,20,35,30]
 
# plot 
plt.pie(x)
plt.show()
Simple matplotlib pie chart

Pie Chart with Numpy Data

You can turn numpy data into pie chart thanks to matplotlib library:

# import packages 
import matplotlib.pyplot as plt
import numpy as np
 
# create data 
x = np.array([35, 25, 25, 15])
 
# plot 
plt.pie(x)
plt.show()
Matplotlib pie chart with numpy data.

Pie Chart with Pandas Data

Display pandas data as a pie chart created with matplotlib:

# import packages
import matplotlib.pyplot as plt
import pandas as pd 
 
# create data 
df = pd.DataFrame({'percent': [15,25,35,25]})
 
# plot 
plt.pie(df['percent'])
plt.show()
Matplotlib pie chart with pandas data.

Interactive Pie Chart

Static charts are boring, what about creating an interactive pie chart? It's possible with matplotlib and mercury packages. You can decide if label will be visible using mercury widgets. In this example we used CheckBox (opens in a new tab):

# import packages 
import matplotlib.pyplot as plt
import mercury as mr
# mercury widget 
choice = mr.Checkbox(value=True, label="Show label")
# create data
x = [20,30,50]
labels = ["Apples", "Bananas", "Oranges"] if choice.value else None
 
# plot
plt.pie(x, labels=labels)
plt.show()

Now, you can turn your Jupyter Notebook into Web App without additional code changes! Here is a video which presents how it will look:

Deploying Web App is very easy that you can do it in 3 steps:

Login to Mercury Cloud

If you don't have account, you can create it here: Mercury Cloud (opens in a new tab).

Create new site

Create new or use an existing site.

Upload your notebook

Upload the notebook with code.

Congrats! You just created your own Web App and you can share your Jupyter Notebooks with nontechnical users. If you need more information about deploying the Web App check Mercury Cloud Documentation (opens in a new tab).