Having Problem of Pyplot Feature on Streamlit

#Pie chart
import pylab
df_count_percent = (df_count['count_of_purchase/campaign'] / df_count['count_of_purchase/campaign'].sum()) * 100
plot0 = df_count_percent.plot.pie(y='count_of_purchase/campaign', fontsize=17,autopct='%0.2f%%',pctdistance=1.2,


plt.title('Count of Purchase or Campaign as Percentage (Pie)', fontsize=19,fontweight="bold",pad=6)

plt.legend(loc='upper right', fontsize=9,prop={'size': 17}, bbox_to_anchor=(2,1))
plot0=plt.figure(figsize=(16, 16))

# Plot a bar chart
figure_0=df_count.plot.barh( y="count_of_purchase/campaign",

plt.title('Count of Purchase or Campaign', fontsize=16,fontweight="bold",pad=12)
# Plot a bar chart as percentage
df_count_percent = (df_count['count_of_purchase/campaign'] / df_count['count_of_purchase/campaign'].sum()) * 100
fig=df_count_percent.plot.barh( y="count_of_purchase/campaign",fontsize=15 ,color='red',edgecolor="#111111")

plt.title('Count of Purchase or Campaign as Percentage',fontsize=16,fontweight="bold",pad=12) 

fig = plt.figure(figsize = (18, 18))

Hello, I’m trying to plot the above three graphs on streamlit (pie and bar graphs), but the circle graph and the y=count_of_purchase/campaign graph are blank. I tried both code for streamlit: st.pyplot(fig) and st.write (fig). How can I plot these truly on streamlit? What is the problem, I don’t know? Normally, the code is working on spyder/jupyter but the problem is with the interface. I couldn’t show it on streamlit. I am waiting for your helps…

Hello, @murat_tasci!

My preferred solution for adding pyplot figures to my streamlit apps is to use plt.subplots() to get a fig, and then pass that fig to st.pyplot. See the example in the documentation: st.pyplot - Streamlit Docs

Hello @blackary I tried subplot instead of plt.figure (…) for pie and bar chart like this and the other codes same as above: Example: plot0=plt.subplots() and st.pyplot(plot0) but I got this error: AttributeError:tuple object has no attribute savefig.

Here’s how you can use st.pyplot() to display Pandas dataframe plots (pandas.DataFrame.plot):

st.pyplot expects a Matplotlib Figure object. That object can be accessed by adding a .figure attribute to df_count_percent.plot.barh(y="count_of_purchase/campaign",fontsize=15,color='red',edgecolor="#111111")

Thank you @snehankekre it worked, but I still have 2 problems.

Firstly, I run the fixed code on Spyder and it shows Count of Purchase or Campaign (bar plot 1) and Count of Purchase or Campaign as Percentage (bar plot 2) differently. This is true thing which I am expected like this because bar plot 1 is normal values, bar plot 2 is bar plot 1’s percentage version. But in the streamlit interface these bar plottings seem to be as same.

Secondly, Can I change the color of border of plotting and box width of plotting?

How can I fix these problems?

Just as a general note, use of matplotlib.pylab is strongly discouraged :melting_face: (API Reference — Matplotlib 3.5.3 documentation). But you can easily rewrite those plots using the object-oriented API (e.g., the fig,ax = plt.subplots() syntax) which allow you to have full control over your plots, to pass the correct figure object to streamlit, connect to pandas, and get the styling you are looking for.

A good starting point is to get familiar with the Anatomy of a figure (Anatomy of a figure — Matplotlib 3.5.3 documentation), that way, you’ll know exactly what to search for within the matplotlib’s documentation.


Hi @murat_tasci, you’ll notice in the example code

fig, ax = plt.subplots()

That subplots returns two objects, a figure, and an axis. In your code,

plot0 is a tuple containing both objects (fig, ax), so it won’t work when trying to display it.