| Function signature[source] | |
|---|---|
st.pyplot(fig=None, clear_figure=None, use_container_width=True, **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.
|
use_container_width (bool) | If True, set the chart width to the column width. Defaults to True. |
**kwargs (any) | Arguments to pass to Matplotlib's savefig function. |
Example
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":
For 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. 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.
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)
Still have questions?
Our forums are full of helpful information and Streamlit experts.
