Seanborn Doesnt render faster-Is too slow on streamlit

Dear Streamlit Community,

Please which is the best chart to use with streamlit…I have tried seaborn and other charts but seaborn seems to be very slow with streamlit…orendously slow.

Kindly help out

1 Like

Hello @kareemrasheed89

There was a small benchmark done by @theimposingdwarf here: Plot Library Speed Trial that suggests using Altair or Plotly rather than seaborn/matploblib. You could try those.

If you are able to share a code snippet maybe we can see if there’s a way to optimize the seaborn part too.

Have a nice day,
Fanilo :balloon:

2 Likes

Many thanks @andfanilo I have read many of articles in this community but I for my case, seaborn is the best i can use for heatmap…in my app, i have mixture of plotly and seaborn but it takes longer than expected to render.

columns=[' OOS/premier_milk___honey___175g',' OOS/premier_aloevera___glyc___175g',' OOS/premier_rose_water___glyc___175g',	 ' OOS/premier_shear_butter___glyc___175g',
        ' OOS/premier_lemon___glyc___175g', ' OOS/premier_lemon___glyc___60g',	 ' OOS/prem_cool_ult____65g',	 ' OOS/prem_cool_sport___65g',	 ' OOS/prem_cool_o__def___65g',
        ' OOS/prem_cool_ult____110g',	 ' OOS/prem_cool_sport___110g',	' OOS/prem_cool_o__def___110g', ' OOS/carex_soap___110g',
        ' OOS/carex_soap___70g', ' OOS/joy_skin_care_soap__tender____150g']
        col1, col2, col3=st.columns([2,0.1,2])
        w_dataoos1=w_data[columns]
        fig=plt.figure(figsize=(10,15))
        sns.heatmap(data=w_dataoos1, annot=True)
        plt.title("Antiseptic/Fragrance Soaps In Last 3Months[retailscope]")
        col1.pyplot(fig)
        columns=[' OOS/joy_skin_care_soap__exfoliating____150g',	 ' OOS/joy_skin_care_soap__tender____70g',
        ' OOS/imp__leather_classic___150g', ' OOS/imp__leather_classic___60g',	 ' OOS/prem_asl_ori__250ml___250ml',	 ' OOS/prem_asl_lf_250ml___250ml',
        ' OOS/prem_asl_ori__500ml___500ml',	 ' OOS/prem_asl_lf_500ml___500ml',	 ' OOS/carex_hwl_moisture_plus___250ml',	 ' OOS/carex_hwl_complete_protect___250ml',
        ' OOS/carex_hwl_herbal_protect___250ml',	 ' OOS/carex_hand_sanitizer___100ml',	 ' OOS/carex_hand_sanitizer___400ml']
        w_dataoos2=w_data[columns]
        fig2=plt.figure(figsize=(10,15))
        sns.heatmap(data=w_dataoos2, annot=True)
        plt.title("Antiseptic/Fragrance Soaps In Last 3Months[retailscope]")
        col3.pyplot(fig2)

I will be happy to get any help to render faster, also i thought of caching by using @st.cache.

Not sure of best way thou

1 Like

I have stumbled upon a good solution without changing to a different library: simply resize the seaborn figure. Streamlit anyway rescales the figures somehow (to match screen width presumably?), so reducing the figure size on seaborn eventaully ends up as controlling the resolution of the image, hence the umber of pixels, memory usage, and speed of upload.
Obviously, you would also need to rescale font size and labels so that they have the same scale as the figure.
In my own code, I set a scale parameter in the config, which I set to a small number (<1. In my case I set it to: scale = 0.2). Then I used something like:

fig = plt.figure(figsize=(scale*24, scale*len(df.columns)))  #scale the desired size, which in my case is (24, len(df.columns))
sns.set(font_scale=scale*2)

sns.heatmap(df, annot=True,  annot_kws={"fontsize": scale*16, "weight": "bold"})

labels_fontsize = scale * 30
plt.title(title, fontsize=labels_fontsize) # title with fontsize 20
plt.xlabel(xlabel, fontsize=labels_fontsize) # x-axis label with fontsize 15
plt.ylabel(ylabel, fontsize=labels_fontsize) # y-axis label with fontsize 15

The main point is that the scale parameter is used throughout to keep proportions.

1 Like

Thanks @Roy_Rosemarin I have technically moved long time ago to Plotly and Echart. No matter what, they render faster than matplotlib libraries. More so, they aint interactive

1 Like

I also happen to use pandas.io.formats.style.Styler.background_gradient — pandas 1.5.0 documentation now instead of matplotlib or seaborn heatmaps! So you can simply

st.dataframe(df.style.background_gradient(axis=None))
1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.