From b3755e91ced0f5df3f9aa681d1a7a83ceb98d2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20D=C3=BCggelin?= Date: Wed, 19 Feb 2025 10:47:03 +0100 Subject: [PATCH 1/2] Update pyplot.md using RLock --- content/develop/api-reference/charts/pyplot.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/develop/api-reference/charts/pyplot.md b/content/develop/api-reference/charts/pyplot.md index 723f95252..704ce81c0 100644 --- a/content/develop/api-reference/charts/pyplot.md +++ b/content/develop/api-reference/charts/pyplot.md @@ -10,8 +10,8 @@ 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. ```python - from matplotlib.backends.backend_agg import RendererAgg - _lock = RendererAgg.lock + from threading import RLock + _lock = RLock() with _lock: fig.title('This is a figure)') From 1712ff724339ee74ed830e4551150cfed53808c4 Mon Sep 17 00:00:00 2001 From: Debbie Matthews Date: Thu, 6 Mar 2025 14:23:29 -0800 Subject: [PATCH 2/2] Executable example and link to docs --- content/develop/api-reference/charts/pyplot.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/content/develop/api-reference/charts/pyplot.md b/content/develop/api-reference/charts/pyplot.md index 704ce81c0..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 + 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) ```