PyplotGlobalUseWarning

Hello.
I’m using seaborn to plot a chart. The code works fine. But, I`m getting this warning:

**PyplotGlobalUseWarning** : You are calling  `st.pyplot()`  without any arguments. After December 
 1st, 2020, we will remove the ability to do this as it requires the use of Matplotlib's global figure 
 object, which is not thread-safe.

I’ve been looking for some answers. But I could find it.
I’m following the instruction, but I’m don’t know what is wrong.

How can I figure it out?

Many thanks.

Hi @Racchel, welcome to the Streamlit community!

That warning is telling you to put your plot object as an argument in st.pyplot. What does your code look like right now that’s throwing the warning?

Hi @randyzwitch.

Here is my code:

fig, ax = plt.figure(figsize=(15,5))
ax = (sns.lineplot(x="DIAS_RESTANTES_PARA_VIAGEM", y="min", data=df_voos_originais, ci=None, palette="muted", label='min'),
sns.lineplot(x="DIAS_RESTANTES_PARA_VIAGEM", y="max", data=df, ci=None, palette="muted", label='max'),
plt.title(titulo, fontweight="bold", fontsize=16, pad=20),
plt.ylabel('Cost'))
st.pyplot(fig)

I’m following the instruction, but I’m don’t know what is wrong.
I tried that code as mentioned st.set_option('deprecation.showPyplotGlobalUse', False) . But I’m still getting that warning.

For some reason this line is working: st.set_option('deprecation.showPyplotGlobalUse', False)

Hey @Racchel. I might be able to help suppress the error without setting that option. If you feel it’s okay to suppress the deprecation warning, that’s fine. However, it’s possible we can change the script and undo the suppression.

Like @randyzwitch says, the warning pops up because an argument is not being sent to st.pyplot. When the argument isn’t specified, we internally call plt.savefig rather than on the figure itself. This works so long as you are looking at the graph on a single browser tab. No other tabs should be open on that page, and, if deployed online, multiple people will not be able to access it at the same time.

I tried the code you provided, but an error appears unrelated.

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.figure(figsize=(15,5))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable Figure object

I think to fix this we need to use the plt.subplots command.

fig, ax = plt.subplots()
# ...
st.pyplot(fig)

Using plt.subplots(), we now need to do operations on ax instead of plt. For seaborn, most plotting have an ax command to perform the operations on (instead of plt). Lastly, there’s a slight change in setting the title and y_label.

I can’t test this code because I do not have the data, but I think the following changes will work. Note the use of ax in each line.

fig, ax = plt.subplots(figsize=(15,5))

# Seaborn Operations (Note ax=ax)
sns.lineplot(x="DIAS_RESTANTES_PARA_VIAGEM", y="min", data=df_voos_originais, ci=None, palette="muted", label='min', ax=ax)
sns.lineplot(x="DIAS_RESTANTES_PARA_VIAGEM", y="max", data=df, ci=None, palette="muted", label='max', ax=ax)

# See Operations on Axes https://matplotlib.org/3.1.1/api/axes_api.html
ax.set_title(titulo, fontweight="bold", fontsize=16, pad=20),
ax.set_ylabel('Cost'))

# Now we can send the fig to pyplot
st.pyplot(fig)

If the above works, then you should be able to remove st.set_option('deprecation.showPyplotGlobalUse', False) And…fingers crossed…no warning should appear!

I hope that helps!

Hi @kmcgrady.

Thanks for your time and a great explanation.
Everything is working.

Once again, many thanks.