Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Seaborn Tutorial

Seaborn Tutorial

Seaborn is a Python data visualization library built on top of Matplotlib, focused on drawing various statistical graphics to make it easier to present and understand data.

Seaborn's design goal is to simplify the process of statistical data visualization, providing a high-level interface and attractive default themes, enabling users to create complex graphics with a small amount of code.

Seaborn provides simple high-level interfaces that make it easy to draw various statistical graphics, including scatter plots, line plots, bar charts, heatmaps, and more, with good aesthetic results.

Seaborn was designed with aesthetics in mind; its default themes and color palettes are carefully chosen to make plots more attractive.

Install Seaborn:



pip install seaborn

Seaborn provides a variety of built-in themes and color palettes, which can be changed through settings to alter the appearance of graphics.

Example

import

seaborn

as

sns

Set themes and color palettes

sns.

set_theme

(

style

=

"whitegrid"

,

palette

=

"pastel"

)

By setting the sns.set_theme() function, you can choose different themes and templates. The following are some built-in Seaborn themes and templates:

Theme

darkgrid (default): dark grid theme.

import seaborn as sns



# Set to darkgrid theme

sns.set_theme(style="darkgrid")

whitegrid: light grid theme.



import seaborn as sns



# Set to whitegrid theme

sns.set_theme(style="whitegrid")

dark: dark theme, no grid.



import seaborn as sns



# Set to dark theme

sns.set_theme(style="dark")

white: light theme, no grid.



import seaborn as sns



# Set to white theme

sns.set_theme(style="white")

ticks: dark theme with tick marks.



import seaborn as sns



# Set to ticks theme

sns.set_theme(style="ticks")

Context

paper: suitable for small figures, with smaller labels and lines.



import seaborn as sns



# Set to paper template

sns.set_theme(context="paper")

notebook (default): suitable for laptops and similar environments, with medium-sized labels and lines.



import seaborn as sns



# Set as notebook template

sns.set_theme(context="notebook")

talk: suitable for presentation slides, with large-sized labels and lines.



import seaborn as sns



# Set to talk template

sns.set_theme(context="talk")

poster: suitable for posters, with very large labels and lines.

import seaborn as sns



# Set to poster template

sns.set_theme(context="poster")

By setting different themes and templates, you can adjust the size, line thickness, colors, and other properties of Seaborn graphics to suit different plotting scenarios. These built-in themes and templates make it easier for users to create attractive and consistent graphics.

The following example uses Seaborn and Matplotlib to draw a simple bar chart showing the sales of different products:

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

Set themes and color palettes

sns.

set_theme

(

style

=

"darkgrid"

,

palette

=

"pastel"

)

Sample data

products

=

[

"Product A"

,

"Product B"

,

"Product C"

,

"Product D"

]

sales

=

[

120

,

210

,

150

,

180

]

Create the bar chart

sns.

barplot

(

x

=

products

,

y

=

sales

)

Add labels and title

plt.

xlabel

(

"Products"

)

plt.

ylabel

(

"Sales"

)

plt.

title

(

"Product Sales by Category"

)

Display the chart

plt.

show

(

)

The result is shown in the following figure:


Plotting functions

Seaborn provides multiple plotting functions for creating various statistical graphics. The following are some of Seaborn's main plotting functions and corresponding examples:

1. Scatter plot - sns.scatterplot()

Used to draw a scatter plot between two variables, with an optional trend line.

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

import

pandas

as

pd

Create a sample DataFrame

data

=

{

'A'

:

[

1

,

2

,

3

,

4

,

5

]

,

'B'

:

[

5

,

4

,

3

,

2

,

1

]

}

df

=

pd.

DataFrame

(

data

)

Draw a scatter plot

sns.

scatterplot

(

x

=

'A'

,

y

=

'B'

,

data

=

df

)

plt.

show

(

)

The result is shown in the following figure:

2. Line plot - sns.lineplot()

Used to draw a trend line plot of one variable changing with another.

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

import

pandas

as

pd

Create a sample DataFrame

data

=

{

'X'

:

[

1

,

2

,

3

,

4

,

5

]

,

'Y'

:

[

5

,

4

,

3

,

2

,

1

]

}

df

=

pd.

DataFrame

(

data

)

Draw a line chart

sns.

lineplot

(

x

=

'X'

,

y

=

'Y'

,

data

=

df

)

plt.

show

(

)

The result is shown in the following figure:

3. Bar chart - sns.barplot()

Used to draw a bar chart of the mean or other aggregate function of a variable.

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

import

pandas

as

pd

Create a sample DataFrame

data

=

{

'Category'

:

[

'A'

,

'B'

,

'C'

]

,

'Value'

:

[

3

,

7

,

5

]

}

df

=

pd.

DataFrame

(

data

)

Draw a bar chart

sns.

barplot

(

x

=

'Category'

,

y

=

'Value'

,

data

=

df

)

plt.

show

(

)

The result is shown in the following figure:

4. Box plot - sns.boxplot()

Used to draw the distribution of a variable, including median, quartiles, etc.

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

import

pandas

as

pd

Create a sample DataFrame

data

=

{

'Category'

:

[

'A'

,

'A'

,

'B'

,

'B'

,

'C'

,

'C'

]

,

'Value'

:

[

3

,

7

,

5

,

9

,

2

,

6

]

}

df

=

pd.

DataFrame

(

data

)

Draw box plot

sns.

boxplot

(

x

=

'Category'

,

y

=

'Value'

,

data

=

df

)

plt.

show

(

)

The result is shown in the following figure:

5. Heatmap - sns.heatmap()

Used to draw a heatmap of matrix data, commonly used to display correlation matrices.

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

import

pandas

as

pd

Create a sample DataFrame

data

=

{

'A'

:

[

1

,

2

,

3

,

4

,

5

]

,

'B'

:

[

5

,

4

,

3

,

2

,

1

]

}

df

=

pd.

DataFrame

(

data

)

Create a correlation matrix

correlation_matrix

=

df.

corr

(

)

Visualize the correlation matrix with a heatmap

sns.

heatmap

(

correlation_matrix

,

annot

=

True

,

cmap

=

'coolwarm'

,

fmt

=

".2f"

)

plt.

show

(

)

The result is shown in the following figure:

6. Violin plot - sns.violinplot()

Used to show the shape and density estimate of a distribution, combining a box plot and kernel density estimation.

Example

import

seaborn

as

sns

import

matplotlib.

pyplot

as

plt

import

pandas

as

pd

Create a sample DataFrame

data

=

{

'Category'

:

[

'A'

,

'A'

,

'B'

,

'B'

,

'C'

,

'C'

]

,

'Value'

:

[

3

,

7

,

5

,

9

,

2

,

6

]

}

df

=

pd.

DataFrame

(

data

)

Plot a violin chart

sns.

violinplot

(

x

=

'Category'

,

y

=

'Value'

,

data

=

df

)

plt.

show

(

)

The result is shown in the following figure:

For more information, refer to the official tutorial:https://seaborn.pydata.org/tutorial.html