diff --git a/content/develop/api-reference/charts/pyplot.md b/content/develop/api-reference/charts/pyplot.md index 723f95252..ffe098550 100644 --- a/content/develop/api-reference/charts/pyplot.md +++ b/content/develop/api-reference/charts/pyplot.md @@ -7,15 +7,22 @@ description: st.pyplot displays a matplotlib.pyplot figure. - MatplotlibĀ [doesn't work well with threads](https://matplotlib.org/3.3.2/faq/howto_faq.html#working-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. + MatplotlibĀ [doesn't work well with threads](https://matplotlib.org/3.3.2/faq/howto_faq.html#working-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`](https://docs.python.org/3/library/threading.html#rlock-objects) from the `threading` module. ```python - from matplotlib.backends.backend_agg import RendererAgg - _lock = RendererAgg.lock + 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.title('This is a figure)') - fig.plot([1,20,3,40]) + fig, ax = plt.subplots() + ax.scatter(x, y) st.pyplot(fig) ```