Skip to main content

Python Seaborn figure styles

While working on data visualization our goal is to communicate insight properly found in the given data. So while we communicate the important information, styling will and influence how our audience understands the insight that we're trying to convey.

As visualizations are the centre to communicate quantitatively insights to an audience, and it is even more necessary to have figures that catch the attention and draw a viewer in.

Matplotlib Package is highly customizable, but it is hard to know what settings to tweak settings to achieve an attractive plot.

In Seaborn, there is a number of customized themes and a high-level interface for controlling the look of matplotlib figures.

After you have done with the formation and visualization part, Then the last step is to do styling. You can customize the overall look of your figure, using background colors, grids, spines, and ticks, scale plots for different contexts, such as presentations and reports.

Here we will explore three main aspects of customising that is background color, grids, and spines. We will see how these changes can change the meaning of visualization.

Examples

Import the Libraries

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
#Here we will use the datasets which are present by default in the seaborn
data= sns.load_dataset('flights')
data.head(5)

Output:

data

Customising Background

Using whitegrids:

sns.set_style('whitegrid')
sns.displot(x='passengers',data=data)
plt.show()

Output:

white

Using Darkgrid:

sns.set_style('darkgrid')
sns.displot(x='passengers',data=data)
plt.show()

Output:

dark

for style in ['white','dark','ticks']:
    sns.set_style(style) 
    sns.displot(x='passengers',data=data)
    plt.show()

Output:

all

 

Removing Axis:

sns.displot(x='passengers',data=data)
sns.despine(left=True)
plt.show()

Output:

axis

Customising Size:

#setting size
plt.figure(figsize =(12, 3))
sns.displot(x='passengers',data=data)
plt.show()

Output:

size

Setting Palette:

for p in sns.palettes.SEABORN_PALETTES:
    sns.set_palette(p)
    sns.displot(x='passengers',data=data)
    plt.show()

Output:

palette

Using set_context

'''This affects things like the size of the labels, 
lines, and other elements of the plot, but not the overall style.'''
sns.displot(x='passengers',data=data, palette='coolwarm')
sns.set_context('notebook', font_scale = 2)
plt.show()

Output:

set

Submitted by devanshi.srivastava on March 3, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.