Cannot change matplotlib figure size

Hey @benlindsay, :wave:

Thanks for your patience! Here’s a workaround with 2 additional lines of code. Instead of using st.pyplot, you can convert the matplotlib.figure.Figure object into an image that’s held temporarily in memory, and then display it using st.image like so:

import streamlit as st
import matplotlib.pyplot as plt
from io import BytesIO

cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing", "washing"]

width = st.sidebar.slider("plot width", 0.1, 25., 3.)
height = st.sidebar.slider("plot height", 0.1, 25., 1.)

fig, ax = plt.subplots(figsize=(width, height))
ax.plot(activity, dog, label="dog")
ax.plot(activity, cat, label="cat")
ax.legend()

buf = BytesIO()
fig.savefig(buf, format="png")
st.image(buf)

matplotlib-resize

Happy Streamlit-ing! :balloon:
Snehan

8 Likes