Seaborn | Style And Color

Seaborn is a statistical plotting library in python. It has beautiful default styles. This article deals with the ways of styling the different kinds of plots in seaborn.
The ways of styling are as follows:-

Set the background to be white

filter_none

edit
close

play_arrow

link
brightness_4
code

import seaborn as sns 
import matplotlib.pyplot as plt 
  
# load the tips dataset present by default in seaborn
tips = sns.load_dataset('tips'
sns.set_style('white')
  
# make a countplot
sns.countplot(x ='sex', data = tips) 

chevron_right


Output:

Explanation: Given style with the help of countplot and the dataset is present in seaborn by default. load_dataset() function is useed to load the dataset. set_style() function is used for plot styling.



Setting ticks

filter_none

edit
close

play_arrow

link
brightness_4
code

import seaborn as sns
import matplotlib.pyplot as plt
   
tips = sns.load_dataset('tips')
sns.set_style('ticks')
sns.countplot(x ='sex', data = tips, palette ='deep')

chevron_right


Explanation: set_style() function is used for plot styling. It causes ticks to appear on the sides of the plot on setting it as set_style(‘ticks’). palette attribute is used to set the color of the bars. It helps to distinguish between chunks of data.

Removal of spines

filter_none

edit
close

play_arrow

link
brightness_4
code

import seaborn as sns
import matplotlib.pyplot as plt
  
tips = sns.load_dataset('tips')
sns.countplot(x ='sex', data = tips)
sns.despine()

chevron_right


Output

Explanation : despine() is a function that removes the spines from the right and upper portion of the plot by default. sns.despine(left = True) helps remove the spine from the left.

Size and aspect

  • Non grid plot

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import seaborn as sns
    import matplotlib.pyplot as plt
      
    tips = sns.load_dataset('tips')
    plt.figure(figsize =(12, 3))
    sns.countplot(x ='sex', data = tips)

    chevron_right

    
    

    Output

    Explanation : figure() is a matplotlib function used to plot the figures. The figsize is used to set the size of the figure.

  • Grid type plot

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import seaborn as sns
    import matplotlib.pyplot as plt
      
    tips = sns.load_dataset('tips')
    sns.lmplot(x ='total_bill', y ='tip', size = 2, aspect = 4, data = tips)

    chevron_right

    
    

    Output

    Explanation This example shows a regression plot of tips vs the total_bill from the dataset. lmplot stands for linear model plot and is used to create a regression plot. x =’total_bill’ sets the x axis to total_bill. y=’tip’ sets the y axis to tips. size=2 is used to the size(the height)of the plot. aspect is used to set the width keeping the width constant.

Scale and Context

filter_none

edit
close

play_arrow

link
brightness_4
code

import seaborn as sns
import matplotlib.pyplot as plt 
  
tips = sns.load_dataset('tips')
sns.set_context('poster', font_scale = 2)
sns.countplot(x ='sex', data = tips, palette ='coolwarm')

chevron_right


Output

Explanation : set_context() allows us to override default parameters. This affects things like the size of the labels, lines, and other elements of the plot, but not the overall style. The base context is “notebook”, and the other contexts are “paper”, “talk”, and “poster”. font_scale sets the font size



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.