Show API reference for

Display a matplotlib.pyplot figure.

Streamlit renders the figure by calling Matplotlib's savefig with bbox_inches="tight" and dpi=200 so charts look sharp and cropped by default.

Important

You must install matplotlib>=3.0.0 to use this command. You can install all charting dependencies as an extra with Streamlit:

pip install streamlit[charts]
Function signature[source]

st.pyplot(fig, clear_figure=False, *, width="stretch", use_container_width=None, **kwargs)

Parameters

fig (Matplotlib Figure)

The Matplotlib Figure object to render. See https://matplotlib.org/stable/gallery/index.html for examples.

clear_figure (bool)

Whether to clear the figure after rendering it. If this is False (default), the figure is left as-is. If True, Streamlit calls fig.clf() after the figure is displayed.

width ("stretch", "content", or int)

The width of the chart element. This can be one of the following:

  • "stretch" (default): The width of the element matches the width of the parent container.
  • "content": The width of the element matches the width of its content, but doesn't exceed the width of the parent container.
  • An integer specifying the width in pixels: The element has a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

use_container_width (bool)

delete

use_container_width is deprecated and will be removed in a future release. For use_container_width=True, use width="stretch". For use_container_width=False, use width="content".

Whether to override the figure's native width with the width of the parent container. If use_container_width is True (default), Streamlit sets the width of the figure to match the width of the parent container. If use_container_width is False, Streamlit sets the width of the chart to fit its contents according to the plotting library, up to the width of the parent container.

**kwargs (any)

delete

Passing savefig keyword arguments to st.pyplot is deprecated and will be removed in a future version. st.pyplot already uses bbox_inches="tight" and dpi=200 by default. For other savefig options, save the figure yourself and display it with st.image.

Arguments to pass to Matplotlib's savefig function.

Examples

import matplotlib.pyplot as plt
import streamlit as st
from numpy.random import default_rng as rng

arr = rng(0).normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)

st.pyplot(fig)

Matplotlib supports several types of "backends". If you're getting an error using Matplotlib with Streamlit, try setting your backend to "TkAgg":

echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc

For more information, see https://matplotlib.org/faq/usage_faq.html.

priority_high

Warning

MatplotlibĀ doesn't work well with threads. So if you're using Matplotlib you should wrap your code with locks. This Matplotlib bug is more prominent when you deploy and share your apps because you're more likely to get concurrent users then. The following example uses Rlock from the threading module.

Python
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
from threading import RLock

_lock = RLock()

x = np.random.normal(1, 1, 100)
y = np.random.normal(1, 1, 100)

with _lock:
    fig, ax = plt.subplots()
    ax.scatter(x, y)
    st.pyplot(fig)
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.