[go: up one dir, main page]

0% found this document useful (0 votes)
32 views17 pages

Chapter 2

The document provides an overview of using Seaborn for intermediate data visualization, including setting styles, customizing colors, and combining plots. It discusses the use of sns.set() for styling, sns.set_palette() for color palettes, and matplotlib Axes for further customization. Additionally, it includes examples of removing axes and defining custom palettes for effective data representation.

Uploaded by

asmaa outaleb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views17 pages

Chapter 2

The document provides an overview of using Seaborn for intermediate data visualization, including setting styles, customizing colors, and combining plots. It discusses the use of sns.set() for styling, sns.set_palette() for color palettes, and matplotlib Axes for further customization. Additionally, it includes examples of removing axes and defining custom palettes for effective data representation.

Uploaded by

asmaa outaleb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Using Seaborn

Styles
I N T E R M E D I AT E D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mo
Instructor
Setting Styles
Seaborn has default con gurations that can be applied with sns.set()

These styles can override matplotlib and pandas plots as well

sns.set()
df['Tuition'].plot.hist()

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Theme examples with sns.set_style()
for style in ['white','dark','whitegrid','darkgrid','ticks']:
sns.set_style(style)
sns.displot(df['Tuition'])
plt.show()

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Removing axes with despine()
Sometimes plots are improved by removing elements

Seaborn contains a shortcut for removing the spines of a plot

sns.set_style('white')
sns.displot(df['Tuition'])
sns.despine(left=True)

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Let's practice!
I N T E R M E D I AT E D ATA V I S U A L I Z AT I O N W I T H S E A B O R N
Colors in Seaborn
I N T E R M E D I AT E D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mo
Instructor
Defining a color for a plot
Seaborn supports assigning colors to plots using matplotlib color codes

sns.set(color_codes=True)
sns.displot(df['Tuition'], color='g')

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Palettes
Seaborn uses the set_palette() function to de ne a pale e

palettes = ['deep', 'muted', 'pastel', 'bright', 'dark','colorblind']


for p in palettes:
sns.set_palette(p)
sns.displot(df['Tuition'])

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Displaying Palettes
sns.palplot() function displays a pale e

sns.color_palette() returns the current pale e

palettes = ['deep', 'muted', 'pastel', 'bright','dark','colorblind']


for p in palettes:
sns.set_palette(p)
sns.palplot(sns.color_palette())
plt.show()

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Defining Custom Palettes
Circular colors = when the data is not Diverging colors = when both the low and
ordered high values are interesting

sns.palplot(sns.color_palette("Paired", 12)) sns.palplot(sns.color_palette("BrBG", 12))

Sequential colors = when the data has a


consistent range from high to low

sns.palplot(sns.color_palette("Blues", 12))

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Let's practice!
I N T E R M E D I AT E D ATA V I S U A L I Z AT I O N W I T H S E A B O R N
Customizing with
matplotlib
I N T E R M E D I AT E D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mo
Instructor
Matplotlib Axes
Most customization available through matplotlib Axes objects

Axes can be passed to seaborn functions

fig, ax = plt.subplots()
sns.histplot(df['Tuition'], ax=ax)
ax.set(xlabel='Tuition 2013-14')

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Further Customizations
The axes object supports many common customizations

fig, ax = plt.subplots()
sns.histplot(df['Tuition'], ax=ax)
ax.set(xlabel="Tuition 2013-14",
ylabel="Distribution", xlim=(0, 50000),
title="2013-14 Tuition and Fees Distribution")

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Combining Plots
It is possible to combine and con gure multiple plots

fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2,


sharey=True, figsize=(7,4))

sns.histplot(df['Tuition'], stat='density', ax=ax0)


sns.histplot(df.query('State == "MN"')['Tuition'], stat='density', ax=ax1)

ax1.set(xlabel='Tuition (MN)', xlim=(0, 70000))


ax1.axvline(x=20000, label='My Budget', linestyle='--')
ax1.legend()

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Combining Plots

INTERMEDIATE DATA VISUALIZATION WITH SEABORN


Let's practice!
I N T E R M E D I AT E D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

You might also like