Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Plots Decoration | Plotting with Seaborn
Ultimate Visualization with Python

book
Plots Decoration

Setting Style

First, let’s start with setting the style for the plot. seaborn has a set_style() function exactly for this purpose. We are interested in its only required parameter called style. It has several possible values, which are all different styles:

  • 'white' ;

  • 'dark' ;

  • 'whitegrid' ;

  • 'darkgrid' ;

  • 'ticks' .

Feel free to experiment with them:

import seaborn as sns
import matplotlib.pyplot as plt
# Setting the style
sns.set_style('darkgrid')
titanic_df = sns.load_dataset('titanic')
sns.countplot(data=titanic_df, x='class')
plt.show()
1234567
import seaborn as sns import matplotlib.pyplot as plt # Setting the style sns.set_style('darkgrid') titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
copy

Setting Palette

Another option you have is to change the colors of the plot elements in seaborn using the set_palette() function. We'll focus on its only required parameter palette. Here are a few possible palettes:

  • Circular palettes: 'hls' , 'husl' ;

  • Perceptually uniform palettes: 'rocket' , 'magma' , 'mako' , etc;

  • Diverging color palettes: 'RdBu' , 'PRGn' , etc;

  • Sequential color palettes: 'Greys' , 'Blues' , etc.

You can explore more of them here. Once again, feel free to experiment with different palettes:

import seaborn as sns
import matplotlib.pyplot as plt
# Setting the style
sns.set_style('darkgrid')
# Setting the palette
sns.set_palette('magma')
# Loading a built-in dataset of the Titanic passengers
titanic_df = sns.load_dataset('titanic')
sns.countplot(data=titanic_df, x='class')
plt.show()
12345678910
import seaborn as sns import matplotlib.pyplot as plt # Setting the style sns.set_style('darkgrid') # Setting the palette sns.set_palette('magma') # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
copy

Setting Context

There is another function in the seaborn library, set_context(). It affects such aspects as the size of the labels, lines, and other elements of the plot (the overall style is not affected).

Its most important parameter context which can either be a dictionary (dict) of parameters or a name of a preconfigured set (string type).

'notebook' is the default context, other contexts are 'paper', 'talk', and 'poster', which are essentially just versions of the notebook parameters scaled by a certain value.

Here is an example, you can try different contexts and see the difference:

import seaborn as sns
import matplotlib.pyplot as plt
# Setting the style
sns.set_style('darkgrid')
# Setting the palette
sns.set_palette('magma')
# Setting the context
sns.set_context('paper')
# Loading a built-in dataset of the Titanic passengers
titanic_df = sns.load_dataset('titanic')
sns.countplot(data=titanic_df, x='class')
plt.show()
123456789101112
import seaborn as sns import matplotlib.pyplot as plt # Setting the style sns.set_style('darkgrid') # Setting the palette sns.set_palette('magma') # Setting the context sns.set_context('paper') # Loading a built-in dataset of the Titanic passengers titanic_df = sns.load_dataset('titanic') sns.countplot(data=titanic_df, x='class') plt.show()
copy

You can explore more about set_context() in its documentation.

Opgave

Swipe to start coding

  1. Use the correct function to set the style to 'dark'.
  2. Use the correct function to set the palette to 'rocket'.
  3. Use the correct function to set the context to 'talk'.

Løsning

import seaborn as sns
import matplotlib.pyplot as plt
# Set the style
sns.set_style('dark')
# Set the palette
sns.set_palette('rocket')
# Set the context
sns.set_context('talk')
# Loading a built-in dataset of the Titanic passengers
titanic_df = sns.load_dataset('titanic')
sns.countplot(data=titanic_df, x='class')
plt.show()

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 3
import seaborn as sns
import matplotlib.pyplot as plt
# Set the style
___.___(___)
# Set the palette
___.___(___)
# Set the context
___.___(___)
# Loading a built-in dataset of the Titanic passengers
titanic_df = sns.load_dataset('titanic')
sns.countplot(data=titanic_df, x='class')
plt.show()

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt