Hello there 👋

Thanks for stopping by! We use cookies to help us understand how you interact with our website.

By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.

priority_high

Warning

You are reading the documentation for Streamlit version 1.20.0, but 1.41.0 is the latest version available.

Display a matplotlib.pyplot figure.
Function signature[source]

st.pyplot(fig=None, clear_figure=None, **kwargs)

Parameters

fig (Matplotlib Figure)

The figure to plot. When this argument isn't specified, this function will render the global figure (but this is deprecated, as described below)

clear_figure (bool)

If True, the figure will be cleared after being rendered. If False, the figure will not be cleared after being rendered. If left unspecified, we pick a default based on the value of fig.

  • If fig is set, defaults to False.
  • If fig is not set, defaults to True. This simulates Jupyter's approach to matplotlib rendering.

**kwargs (any)

Arguments to pass to Matplotlib's savefig function.

Example

import streamlit as st
import matplotlib.pyplot as plt
import numpy as np

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

st.pyplot(fig)
(view standalone Streamlit app)

Notes

Note

Deprecation warning. After December 1st, 2020, we will remove the ability to specify no arguments in st.pyplot(), as that requires the use of Matplotlib's global figure object, which is not thread-safe. So please always pass a figure object as shown in the example section above.

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 as shown in the snippet below. This Matplotlib bug is more prominent when you deploy and share your app apps since you're more likely to get concurrent users then.

from matplotlib.backends.backend_agg import RendererAgg _lock = RendererAgg.lock with _lock: fig.title('This is a figure)') fig.plot([1,20,3,40]) st.pyplot(fig)
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.