How to draw pie chart with matplotlib.pyplot

I know that fig, ax = plt.subplots() is required. However, what to pass to ax for piechart?

Hi @Keeb_Thock,

I encourage you to checkout the matplotlib documentation for pie charts :slight_smile: There’s an ax.pie(x) function that makes a pie chart of array x, corresponding to the wedge sizes .

Here’s an example adapted from the matplotlib gallery:

import streamlit as st
import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

st.pyplot(fig1)

Output:

Happy Streamlit-ing! :balloon:
Snehan

3 Likes