Function signature[source] | |
---|---|
st.pyplot(fig=None, clear_figure=None, use_container_width=True, **kwargs) | |
Parameters | |
fig (Matplotlib Figure) | The Matplotlib Figure object to render. See https://matplotlib.org/stable/gallery/index.html for examples. Note When this argument isn't specified, this function will render the global Matplotlib figure object. However, this feature is deprecated and will be removed in a later version. |
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.
|
use_container_width (bool) | 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) | 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)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/matplotlibrcFor more information, see https://matplotlib.org/faq/usage_faq.html.
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)
Still have questions?
Our forums are full of helpful information and Streamlit experts.