Seaborn FacetGrid | How to make Small Multiples with Python Seaborn | Titles, Hue, Legend
HTML-код
- Опубликовано: 12 июл 2024
- This Seaborn FacetGrid tutorial shows you how to make and style a FacetGrid using Python Seaborn. I first introduce you to the concept of small multiples and making Seaborn small multiples with the facet grid. We walk through the row and col options, and I then show you how to use the Seaborn FacetGrid map and map_dataframe methods to add plots to the FacetGrid. I demonstrate how to make a seaborn FacetGrid histogram as well as scatter plots. I then teach you how to style the facet grid using methods and attributes including updating the Seaborn FacetGrid title through title templates and adding axes labels. I also show you how to use sharey and ylim within the facet grid. We next talk about displaying additional categorical data through the Seaborn FacetGrid hue, and I also show you how to add a hue legend. I show you code for updating the palette and how to style a Seaborn FacetGrid KDEplot. Finally, I demo a custom Python function that you can use to customize the facet grid however you like!
0:00 Introduction
0:21 Intro to Small Multiples
1:03 FacetGrid Code Basics
5:49 FacetGrid Methods & Attributes (axes labels, titles, sharey, ylim)
10:16 Hue & Custom Functions
15:18 Conclusion
Github Code:
github.com/kimfetti/Videos/bl...
Related Videos:
Seaborn histplot -- • Seaborn histplot | How...
Seaborn scatterplot -- • Seaborn scatter plot |...
Seaborn kdeplot -- • What is kernel density...
Full Intro to Seaborn PLAYLIST -- • Intro to Seaborn
#seaborn #dataviz - Наука
Spent several frustrating days looking for something like this to plug my knowledge gap. Exactly what I needed...world's greatest tutorial!
Right on! Happy you liked it!
Your explanation saves me a lot of time. Thanks a lot.
Excellent - glad to hear it was helpful!
Seaborn FacetGrid is just awesome, and so is this tutorial video! Many thanks!
Right?! Making these recent videos about the relplot, catplot, etc, I am really amazed at all the cool things you can do with the FacetGrid and with so little code! Cheers and thanks for the support 😄
Your videos are of great value, as they are concise, practical and clear. I full-heartedly recommended them on my Twitter feed for quant investors. Keep up the good work!
So glad you are enjoying the videos and thank you very much for the recommendation on Twitter!
You really are saving my life with your videos! Thank you
I have never seen an explanation so beautifully put in a video. Damn! Instant subscribe
Thanks a lot! I now understand FacetGrid! This will be very useful.
Awesome -- glad to hear that! I agree -- the FacetGrid (along with the catplot, relplot, etc.) are super useful. So much cool functionality!
Here you are saving lives again. Or more specifically, hours of life with these thorough but succinct videos..
Totally in love with you and your videos 🙂
Why, thank you! So happy my videos have been helpful and enjoyable! 😄
Kudos for lucid explanation!! Thank you from India.
Awesome - glad you enjoyed it! Cheers!
thank you so much for the teaching. bravo !!
So nicely explained. Thanks a lot.
So glad you liked it - thanks!
Hey thank you so much for your awesome, organised and detailed didatic method! The best I've seen so far on RUclips!
I'd like to make a request. Can you please make a regular expressions series? I'd like to learn how to use them better on strings recognition and text manipulation.
Oh fun - thank you for the suggestion! I enjoy regex pattern building too - especially to help out with string cleaning for NLP or for data extraction during web scraping. I will definitely add it to the list. Glad to hear you are enjoying the content so far!
Thank you! I was struggling with the hue's legend color and couldn't find the solution anywhere. Awesome tutorial btw.
So glad this video provided you with a solution - and thanks!
This is really cool
Thanks that is what I was looking for.
You just saved one life today❤️.
So glad to hear it was helpful!
I love your videos .... thank you
Oh thank you - cheers! 😄
Thank you so much.
Thank you so much from France :)
De rien! Thanks for watching!
Keep it up!!! you're the best....
Thanks! 😄 Will definitely continue making more videos.
Thanks so much
Hey you are great, keep it up!
Hey, thank you! I appreciate the kudos! 😄
Okay, I love Kimberly 😭
Thank you.
Most welcome!
Hi Kimberly, thank you for the video :)
Could you please let me know hoe exactly is the value on the y axis determined when facet grid is used? I have used it a couple of times but never figured how the y axis values are determined.
Thanks.
👍🏻
Hi Kimberly! Thank you for this video. I am finding FacetGrids so useful and easy to use. The only exception is that I created 3 count plots in the FacetGrid and now I am trying to sort the bars by frequency. I created a function for this purpose to insert it in map_dataframe(), but is not working. I have tried multiple things and I cannot get my count plots to be sorted descending so they are visually easier to read. Any ideas? Thank you!
Great video! I spent so many days solving the problems and finally I am here! thank you !
I made some nice graphs but they have low resolution or low image quality when I save them as pictures, do you have any idea of that?
thank you again :)
Oh wonderful! Glad to hear this video helped you. There are a few different ways to save figures. Are you using matplotlib's pyplot function? If so, you can either increase your "dots per inch" or "dpi" with plt.savefig("myfigure.png", dpi=200) - the default is 100 so increasing that will increase your resolution. Or if you are okay with switching to an svg file, that could be increased with no problem since it saves the file with vectors instead of raster dots. So that would be plt.savefig("myfigure.svg").
Hi, I have a question, for the 'col' argument, I have nine values. How can I make it into a grid instead of nine columns in a row? (like a 4x3 grid)
Hi Kymberly, thanks for the video
Can I change a different marker within each graph in the small multiples in Seaborn?
Yes! You can do this, but it's a little tricky. If you are mapping sns.scatterplot to your FacetGrid, you'll need to include both the style and markers keywords. "style" should be set to the column name of your facets (for example, if I do "col='species'" in my FacetGrid, I'll do "style='species'" in my map_dataframe method). Then "markers" should be a dictionary where the keys are the categories in your column of interest and the values are which marker you'd like to use (for example, "markers={'Adelie': 'd', 'Chinstrap': '^', 'Gentoo': 'o'}" if I'm working with the penguin species). The full code might look like this:
g = sns.FacetGrid(penguins, col='species')
g.map_dataframe(sns.scatterplot, x='bill_length_mm', y='bill_depth_mm', style='species', markers={'Adelie': 'd', 'Chinstrap': '^', 'Gentoo': 'o'})
Hope that helps!
@@KimberlyFessel Thanks !!
see ya
Cheers!
why did you add **kws parameter in function add_mean_ line ?
please explain...
I can’t like twice so here we go
👏👏👏
is facetgrid same as pivot table plotting?
Yes, similar idea to the Pivot Chart in Excel. Though the facet grid can be created from the raw data instead of a pivot table.
Hello @Kimberly ...thank for such an awesome video. I have been watching your other videos, they are very helpful. When followed your code exactly, I got an error:
TypeError: add_mean_ln() missing 1 required positional argument: 'data_df'
and got a plot without any horizontal mean line. My code is:
def add_mean_ln( data_df, colm= None, **kws ):
# if no x_var provided skip this altogether
if not colm: return
avg = np.mean( data_df[colm] )
# get current axis
ax = plt.gca()
# add v-line at the grp mean
ax.axvline( avg, color= 'red', ymin=0,ymax=1, lw= 3, ls= '--' )
# annotate grp mean
x_pos = 0.65
if avg > 5000: x_pos = 0.2
ax.text( x_pos,0.7, s= f'mean = {avg:.0f}',
transform= ax.transAxes, # (1,1)
color= 'maroon', fontweight= 'bold', fontsize= 12
)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And this is how I called the facetGrid- using auto_mpg data ( my facetGrid is working well, if I don't use the custom plot function )
fctGrd1 = sns.FacetGrid( data= auto_df, col= 'cyl_class', row= 'wt_class' )
fctGrd1.map_dataframe( sns.kdeplot, x= 'mpg' ) #
Hi there - so glad you found the video useful and that you tried out the code! There's one tiny detail causing this error. The function called "add_mean_ln" needs to take in a positional argument called "data" and NOT "data_df". Strange that it has to be exactly that, but seaborn requires that any function applied via FacetGrid's map_dataframe() MUST have a position argument called exactly "data", which is how it will pass in the data from each facet. Hopefully that clears things up! Cheers!
@@KimberlyFessel Wow thanks, it works like a charm! This creating custom func for plotting will go a long way for me....
Thanks for this video and channel. Your explanation are truly cogent, concise and easy-to-understand. Wish I had a teacher like you during my undergrad.
People like you makes (online) learning possible and pain-free :) Cheers!!
I did type (g) and got “ NameError: name ‘g’ is not defined. But I can direct use g.
Oh noes! Did you execute the cell above where we assign the result of the FacetGrid to the variable g?
How i can change position of titles please?
culmen_depth_mm, culmen_length_mm are not exist but there are bill_depth_mm, bill_length_mm
What is dat.query
Why you stop making videos ?
Get some sleep girl. Dark circles but great tutorial
Haha 😴😴 Appreciate the concern, but alas, my dark circles are a result of allergies and genetics. Glad you liked the tutorial nonetheless.
@@KimberlyFessel you are actually gorgeous the way you are.
please dont add adds in between the lecture as it disturbs the concentration please