4 ways to display Seaborn charts in Streamlit
HTML-код
- Опубликовано: 7 авг 2024
- Stumped by "object has no attribute savefig" when trying to render a Seaborn chart in Streamlit? In this tutorial we will see how the underlying Matplotlib library works and how you can leverage the links between Figure, Axes and Seaborn to render your beautiful charts inside Streamlit
00:00 Intro
00:20 Matplotlib - Figure vs Axes
01:12 How Streamlit deals with Figure
01:29 Method 1
02:09 Method 2
02:29 Method 3
03:12 Method 4
03:52 Outro
📜 Source code: Code: github.com/andfanilo/social-m...
👉 Links
- Anatomy of a Matplotlib chart: matplotlib.org/stable/gallery...
- Seaborn: seaborn.pydata.org/
🐦Follow all my updates on Twitter: / andfanilo
🗣️ My other links: linktr.ee/andfanilo
☕ Want to support me? www.buymeacoffee.com/andfanilo
Want to start the RUclips Grind too? Here are some of my tools
🎵 Music (Epidemic Sound) - www.epidemicsound.com/referra...
👍 Like & Subscribe if you would like to see more videos!
(5th video of the year out of 35. Things are going smoothly :) right now I've got 3 more videos in my backlog so I feel like I can focus on building a longer video!)
External Resources
- Photo by Daniel K Cheung on Unsplash
- Icons made by Pixel perfect, Good Ware from Flaticon (www.flaticon.com)
#streamlit #python #datascience #dataapps
⚠️ Disclaimer: This video is not sponsored. Links included in this description might be affiliate links. If you purchase a product or service with the links that I provide I may receive a small commission. Thank you for supporting my channel so I can continue to provide you with free content each week!
Hi Fanilo - thanks for the tutorial... I'll be using Seaborn now :-) Just wondering if you meant to show fig = ax.get_figure() followed by st.pyplot(fig) a 3:00min in the video?
That's the answer to the problem we encounter at 3:00, I just realized I never actually write it down afterwards :o I guess I'll pin your comment then ;)
Thanks it helped me on my project.
Straight to the point solutions.
Very useful. 👍
Thanks for the support :)
omg thank you thank you thank you 😭🙏✨ this is such a good explanation and it helped me with my problem right away (not me searching the internet up and down for days trying to figure out how to display multiple different seaborn plots in streamlit).
Glad it helped! Did my video appear in a Google search XD ?
Dude this is the most awesome explanation ever!
Glad it helped :) hope to see you on the next one
I have been following all your videos. Great job :)
Can I ask one question here please?
How do I make these charts displayed in Streamlit interactive as in showing the value in a tool tip when hovering the mouse on a particular bar in a bar plot ?
I was stuck with the exact savefig error message, thank you so much for a clear concise tutorial explaining why it happens and how to fix it!
Glad it helped! Thanks for the support :)
This is just the video I needed. Thanks for the tutorial!
Happy streamlitin ;)
Don't know why I haven't seen your channel before. You are explaining things extremely well. And this seems to be caused by an incredible deep understanding of the topics by yourself. Thank you very much for this video!
Thanks for the feedback, I'm very grateful 🙂
Are you currently building a Streamlit app?
@@andfanilo Not something specific. I build software since I am 13, and I am now already a few years a professional. But I just discovered my love for data science and machine learning topics, so I am still reaaaally in the learning phase and I am simply stunned with what streamlit is capable of. :)
Awesome! Well stay motivated and happy Streamlitin', hope to see some of your apps soon :)
Thanks for your informative content. It waa helpful.
Glad it was helpful! Hope to see you around :)
Thanks Fanilo, this video helps me
thank you brother!!
Your explanation helped me a lot.
And I don't even know English!! THOUSAND LIKES
Thank you for watching and for your determination to go through a French-accent English-speaking video 😁
Thanks very much, bro! You've helped me a lot!!
Glad I could help, thanks for watching!
Hi Fanilo ! Many thanks for the tutorial, it is BRILLIANTLY explained, one of the best tutorial I have seen for a long time !
JUst a quick question : how do you do the select box to select the plot to display and change the plot depending on the choice from the select box ?
I do not see it in the source code.
Can't wait to watch more tutorials ;) Cheeeers !
Hey, thanks for the support!
Oh you're right, the source code doesn't include the select box part. If you follow docs.streamlit.io/library/api-reference/widgets/st.selectbox I implemented a method to run for each option of the selectbox, then run an if-else so that for each option of the selectbox I run the corresponding method, so the selected option would run the corresponding plot method (or use a dictionary where the keys are the options and the values are the methods to run)
Hope this helps!
Fanilo
I glad that your subscribers number getting higher :)
Thank you for the support, it's very very appreciated 😄 hope to keep seeing you around here!
Keep making vedios like this and I might just subscribe
👌💪
On which "viz library in Streamlit" subject 😆 ? Seaborn? Matplotlib? Plotly?
Hi Fanilo, thanks for the nice explanation.
Following on your approach, how to change the style of subplots please?
I have tried:
fig, (ax1) = plt.subplots(figsize=(10, 4))
with sns.axes_style({
'axes.labelcolor': 'blue',
}):
sns.countplot(data=df, x=column, ax=ax1)
But, it didn't work.
Hey there,
I think you need to build the plt.subplots inside the sns.axes_style for the figure to take the seaborn styles into account, like the following:
```
import streamlit as st
import seaborn as sns
import matplotlib.pyplot as plt
penguins = sns.load_dataset("penguins")
with sns.axes_style({
'figure.facecolor': 'red',
}):
fig, ax1 = plt.subplots(figsize=(10, 4))
sns.countplot(data=penguins, x="flipper_length_mm", ax=ax1)
st.pyplot(fig)
```
That worked for me, hope it does for you too!
Permission to ask sir, what is the syntax for using waveforms? Because, the project I'm designing is about traffic congestion classification based on sound, so the use of wave paths in this case is very necessary. Thank you
I think I used Matplotlib + librosa for that before, see the first 3 functions in: github.com/andfanilo/streamlit-pedalboard-demo/blob/main/app.py (hopefully it's still up to date)
Your project sounds super interesting, looking forward to seeing the result :) are you going to host it on Streamlit Cloud/Huggingface Spaces?
hello, i have graph empty in streamlit...
fig = plt.figure()
cross_tab_prop_nm.plot(kind='bar',
stacked=True,
color=["blue","royalblue","dodgerblue"],alpha=0.6,figsize=(25, 15),fontsize = 15,width=0.7)
plt.legend(loc="upper center", ncol=3, fontsize= 20,bbox_to_anchor = (0.50, 1.09))
plt.xlabel("PROFESIONALES NO MEDICOS", fontsize = 20)
plt.ylabel("PROPORCION", fontsize = 20)
st.pyplot(fig)
HELP ME!!!!!! :( plssssssss
Hello Matias,
I recommend in the video to not use the plt/global state matplotlib object and learn the object oriented version instead. The plot function returns you a new ax object you can use and manipulate, then get back the figure for Streamlit:
```
speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
'lifespan': lifespan}, index=index)
ax = df.plot(
kind='bar',
stacked=True,
alpha=0.6,
figsize=(25, 15),
fontsize = 15,
width=0.7,
)
ax.legend(loc="upper center", ncol=3, fontsize= 20,bbox_to_anchor = (0.50, 1.09))
ax.set_xlabel("PROFESIONALES NO MEDICOS", fontsize = 20)
ax.set_ylabel("PROPORCION", fontsize = 20)
st.pyplot(ax.get_figure())
```
Fanilo
@@andfanilo Thanks friend, from chilean :D