Skip to content

Commit 1ae6c6a

Browse files
Update pyplot docs using RLock (#1230)
* Update pyplot.md using RLock * Executable example and link to docs --------- Co-authored-by: Debbie Matthews <[email protected]>
1 parent a513411 commit 1ae6c6a

File tree

1 file changed

+12
-5
lines changed
  • content/develop/api-reference/charts

1 file changed

+12
-5
lines changed

content/develop/api-reference/charts/pyplot.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ description: st.pyplot displays a matplotlib.pyplot figure.
77
<Autofunction function="streamlit.pyplot" />
88

99
<Warning>
10-
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.
10+
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.
1111

1212
```python
13-
from matplotlib.backends.backend_agg import RendererAgg
14-
_lock = RendererAgg.lock
13+
import streamlit as st
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
from threading import RLock
17+
18+
_lock = RLock()
19+
20+
x = np.random.normal(1, 1, 100)
21+
y = np.random.normal(1, 1, 100)
1522

1623
with _lock:
17-
fig.title('This is a figure)')
18-
fig.plot([1,20,3,40])
24+
fig, ax = plt.subplots()
25+
ax.scatter(x, y)
1926
st.pyplot(fig)
2027
```
2128

0 commit comments

Comments
 (0)