Using pyplot and pandas to display a horizontal stacked bar plot

Hi @bemortz, welcome to the Streamlit community! :wave: :partying_face:

st.pyplot expects a Matplotlib Figure object. That object can be accessed by adding a .figure attribute to df.plot.barh(stacked=True):

import pandas as pd
import streamlit as st

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ["snail", "pig", "elephant", "rabbit", "giraffe", "coyote", "horse"]
df = pd.DataFrame({"speed": speed, "lifespan": lifespan}, index=index)

st.pyplot(df.plot.barh(stacked=True).figure)

1 Like