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!

Комментарии • 37

  • @sehmiconscious
    @sehmiconscious 2 года назад +1

    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?

    • @andfanilo
      @andfanilo  2 года назад +1

      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 ;)

  • @loiclequere9383
    @loiclequere9383 Год назад +1

    Thanks it helped me on my project.
    Straight to the point solutions.
    Very useful. 👍

    • @andfanilo
      @andfanilo  Год назад

      Thanks for the support :)

  • @moskitolove
    @moskitolove 3 месяца назад

    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).

    • @andfanilo
      @andfanilo  3 месяца назад

      Glad it helped! Did my video appear in a Google search XD ?

  • @AbhishekShivkumar-ti6ru
    @AbhishekShivkumar-ti6ru 8 месяцев назад +1

    Dude this is the most awesome explanation ever!

    • @andfanilo
      @andfanilo  8 месяцев назад

      Glad it helped :) hope to see you on the next one

    • @AbhishekShivkumar-ti6ru
      @AbhishekShivkumar-ti6ru 7 месяцев назад

      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 ?

  • @boopskaboop7221
    @boopskaboop7221 6 месяцев назад

    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!

    • @andfanilo
      @andfanilo  6 месяцев назад +1

      Glad it helped! Thanks for the support :)

  • @14ShadO
    @14ShadO 2 года назад +3

    This is just the video I needed. Thanks for the tutorial!

    • @andfanilo
      @andfanilo  2 года назад

      Happy streamlitin ;)

  • @timdithmer9184
    @timdithmer9184 Год назад

    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!

    • @andfanilo
      @andfanilo  Год назад

      Thanks for the feedback, I'm very grateful 🙂
      Are you currently building a Streamlit app?

    • @timdithmer9184
      @timdithmer9184 Год назад

      @@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. :)

    • @andfanilo
      @andfanilo  Год назад

      Awesome! Well stay motivated and happy Streamlitin', hope to see some of your apps soon :)

  • @saeidkazemi5446
    @saeidkazemi5446 Год назад

    Thanks for your informative content. It waa helpful.

    • @andfanilo
      @andfanilo  Год назад

      Glad it was helpful! Hope to see you around :)

  • @jackychan4640
    @jackychan4640 Год назад

    Thanks Fanilo, this video helps me

  • @marlonmartins9460
    @marlonmartins9460 Год назад

    thank you brother!!
    Your explanation helped me a lot.
    And I don't even know English!! THOUSAND LIKES

    • @andfanilo
      @andfanilo  Год назад +1

      Thank you for watching and for your determination to go through a French-accent English-speaking video 😁

  • @joaohernandes8338
    @joaohernandes8338 2 года назад

    Thanks very much, bro! You've helped me a lot!!

    • @andfanilo
      @andfanilo  2 года назад

      Glad I could help, thanks for watching!

  • @buddhabob359
    @buddhabob359 Год назад

    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 !

    • @andfanilo
      @andfanilo  Год назад +1

      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

  • @diy__diy
    @diy__diy 2 года назад

    I glad that your subscribers number getting higher :)

    • @andfanilo
      @andfanilo  2 года назад

      Thank you for the support, it's very very appreciated 😄 hope to keep seeing you around here!

  • @nelsonmungai8148
    @nelsonmungai8148 Год назад

    Keep making vedios like this and I might just subscribe
    👌💪

    • @andfanilo
      @andfanilo  Год назад

      On which "viz library in Streamlit" subject 😆 ? Seaborn? Matplotlib? Plotly?

  • @thiagogatti6325
    @thiagogatti6325 10 месяцев назад

    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.

    • @andfanilo
      @andfanilo  10 месяцев назад

      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!

  • @iranikd
    @iranikd Год назад

    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

    • @andfanilo
      @andfanilo  Год назад

      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?

  • @matiasortizortiz4445
    @matiasortizortiz4445 Год назад

    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

    • @andfanilo
      @andfanilo  Год назад

      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

    • @matiasortizortiz4445
      @matiasortizortiz4445 Год назад

      @@andfanilo Thanks friend, from chilean :D