Changing plot style and color IN TRODUCTION TO S EABORN Erin Case - - PowerPoint PPT Presentation

changing plot style and color
SMART_READER_LITE
LIVE PREVIEW

Changing plot style and color IN TRODUCTION TO S EABORN Erin Case - - PowerPoint PPT Presentation

Changing plot style and color IN TRODUCTION TO S EABORN Erin Case Data Scientist Why customize? Reasons to change style: Personal preference Improve readability Guide interpretation INTRODUCTION TO SEABORN Changing the gure style


slide-1
SLIDE 1

Changing plot style and color

IN TRODUCTION TO S EABORN

Erin Case

Data Scientist

slide-2
SLIDE 2

INTRODUCTION TO SEABORN

Why customize?

Reasons to change style: Personal preference Improve readability Guide interpretation

slide-3
SLIDE 3

INTRODUCTION TO SEABORN

Changing the gure style

Figure "style" includes background and axes Preset options: "white", "dark", "whitegrid", "darkgrid", "ticks"

sns.set_style()

slide-4
SLIDE 4

INTRODUCTION TO SEABORN

Default gure style ("white")

sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-5
SLIDE 5

INTRODUCTION TO SEABORN

Figure style: "whitegrid"

sns.set_style("whitegrid") sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-6
SLIDE 6

INTRODUCTION TO SEABORN

Other styles

sns.set_style("ticks") sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-7
SLIDE 7

INTRODUCTION TO SEABORN

Other styles

sns.set_style("dark") sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-8
SLIDE 8

INTRODUCTION TO SEABORN

Other styles

sns.set_style("darkgrid") sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-9
SLIDE 9

INTRODUCTION TO SEABORN

Changing the palette

Figure "palette" changes the color of the main elements of the plot

sns.set_palette()

Use preset palettes or create a custom palette

slide-10
SLIDE 10

INTRODUCTION TO SEABORN

Diverging palettes

slide-11
SLIDE 11

INTRODUCTION TO SEABORN

Example (default palette)

category_order = ["No answer", "Not at all", "Not very", "Somewhat", "Very"] sns.catplot(x="how_masculine", data=masculinity_data, kind="count",

  • rder=category_order)

plt.show()

slide-12
SLIDE 12

INTRODUCTION TO SEABORN

Example (diverging palette)

sns.set_palette("RdBu") category_order = ["No answer", "Not at all", "Not very", "Somewhat", "Very"] sns.catplot(x="how_masculine", data=masculinity_data, kind="count",

  • rder=category_order)

plt.show()

slide-13
SLIDE 13

INTRODUCTION TO SEABORN

Sequential palettes

slide-14
SLIDE 14

INTRODUCTION TO SEABORN

Sequential palette example

slide-15
SLIDE 15

INTRODUCTION TO SEABORN

Custom palettes

custom_palette = ["red", "green", "orange", "blue", "yellow", "purple"] sns.set_palette(custom_palette)

slide-16
SLIDE 16

INTRODUCTION TO SEABORN

Custom palettes

custom_palette = ['#FBB4AE', '#B3CDE3', '#CCEBC5', '#DECBE4', '#FED9A6', '#FFFFCC', '#E5D8BD', '#FDDAEC', '#F2F2F2'] sns.set_palette(custom_palette)

slide-17
SLIDE 17

INTRODUCTION TO SEABORN

Changing the scale

Figure "context" changes the scale of the plot elements and labels

sns.set_context()

Smallest to largest: "paper", "notebook", "talk", "poster"

slide-18
SLIDE 18

INTRODUCTION TO SEABORN

Default context: "paper"

sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-19
SLIDE 19

INTRODUCTION TO SEABORN

Larger context: "talk"

sns.set_context("talk") sns.catplot(x="age", y="masculinity_important", data=masculinity_data, hue="feel_masculine", kind="point") plt.show()

slide-20
SLIDE 20

Let's practice!

IN TRODUCTION TO S EABORN

slide-21
SLIDE 21

Adding titles and labels: Part 1

IN TRODUCTION TO S EABORN

Erin Case

Data Scientist

slide-22
SLIDE 22

INTRODUCTION TO SEABORN

Creating informative visualizations

slide-23
SLIDE 23

INTRODUCTION TO SEABORN

FacetGrid vs. AxesSubplot objects

Seaborn plots create two different types of objects: FacetGrid and AxesSubplot

g = sns.scatterplot(x="height", y="weight", data=df) type(g) > matplotlib.axes._subplots.AxesSubplot

slide-24
SLIDE 24

INTRODUCTION TO SEABORN

An Empty FacetGrid

slide-25
SLIDE 25

INTRODUCTION TO SEABORN

FacetGrid vs. AxesSubplot objects

Object Type Plot Types Characteristics

FacetGrid relplot() , catplot()

Can create subplots

AxesSubplot scatterplot() , countplot() , etc. Only creates a single plot

slide-26
SLIDE 26

INTRODUCTION TO SEABORN

Adding a title to FacetGrid

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box") g.fig.suptitle("New Title") plt.show()

slide-27
SLIDE 27

INTRODUCTION TO SEABORN

Adjusting height of title in FacetGrid

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box") g.fig.suptitle("New Title", y=1.03) plt.show()

slide-28
SLIDE 28

Let's practice!

IN TRODUCTION TO S EABORN

slide-29
SLIDE 29

Adding titles and labels: Part 2

IN TRODUCTION TO S EABORN

Erin Case

Data Scientist

slide-30
SLIDE 30

INTRODUCTION TO SEABORN

Adding a title to AxesSubplot

FacetGrid

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box") g.fig.suptitle("New Title", y=1.03)

AxesSubplot

g = sns.boxplot(x="Region", y="Birthrate", data=gdp_data) g.set_title("New Title", y=1.03)

slide-31
SLIDE 31

INTRODUCTION TO SEABORN

Titles for subplots

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box", col="Group")

slide-32
SLIDE 32

INTRODUCTION TO SEABORN

Titles for subplots

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box", col="Group") g.fig.suptitle("New Title", y=1.03)

slide-33
SLIDE 33

INTRODUCTION TO SEABORN

Titles for subplots

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box", col="Group") g.fig.suptitle("New Title", y=1.03) g.set_titles("This is {col_name}")

slide-34
SLIDE 34

INTRODUCTION TO SEABORN

Adding axis labels

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box") ` g.set(xlabel="New X Label", ylabel="New Y Label") plt.show()

slide-35
SLIDE 35

INTRODUCTION TO SEABORN

Rotating x-axis tick labels

g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box") plt.xticks(rotation=90) plt.show()

slide-36
SLIDE 36

Let's practice!

IN TRODUCTION TO S EABORN

slide-37
SLIDE 37

Putting it all together

IN TRODUCTION TO S EABORN

Erin Case

Data Scientist

slide-38
SLIDE 38

INTRODUCTION TO SEABORN

Getting started

T

  • import Seaborn:

import seaborn as sns

T

  • import Matplotlib:

import matplotlib.pyplot as plt

T

  • show a plot:

plt.show()

slide-39
SLIDE 39

INTRODUCTION TO SEABORN

Relational plots

Show the relationship between two quantitative variables Examples: scatter plots, line plots

sns.relplot(x="x_variable_name", y="y_variable_name", data=pandas_df, kind="scatter")

slide-40
SLIDE 40

INTRODUCTION TO SEABORN

Categorical plots

Show the distribution of a quantitative variable within categories dened by a categorical variable Examples: bar plots, count plots, box plots, point plots

sns.catplot(x="x_variable_name", y="y_variable_name", data=pandas_df, kind="bar")

slide-41
SLIDE 41

INTRODUCTION TO SEABORN

Adding a third variable (hue)

Setting hue will create subgroups that are displayed as different colors on a single plot.

slide-42
SLIDE 42

INTRODUCTION TO SEABORN

Adding a third variable (row/col)

Setting row and/or col in relplot() or

catplot() will create subgroups that are

displayed on separate subplots.

slide-43
SLIDE 43

INTRODUCTION TO SEABORN

Customization

Change the background: sns.set_style() Change the main element colors: sns.set_palette() Change the scale: sns.set_context()

slide-44
SLIDE 44

INTRODUCTION TO SEABORN

Adding a title

Object Type Plot Types How to Add Title

FacetGrid relplot() , catplot() g.fig.suptitle() AxesSubplot scatterplot() , countplot() , etc. g.set_title()

slide-45
SLIDE 45

INTRODUCTION TO SEABORN

Final touches

Add x- and y-axis labels:

g.set(xlabel="new x-axis label", ylabel="new y-axis label")

Rotate x-tick labels:

plt.xticks(rotation=90)

slide-46
SLIDE 46

Let's practice!

IN TRODUCTION TO S EABORN

slide-47
SLIDE 47

Well done! What's next?

IN TRODUCTION TO S EABORN

Erin Case

Data Scientist

slide-48
SLIDE 48

INTRODUCTION TO SEABORN

Where does Seaborn t in?

slide-49
SLIDE 49

INTRODUCTION TO SEABORN

Where does Seaborn t in?

slide-50
SLIDE 50

INTRODUCTION TO SEABORN

Next Steps: Explore and communicate results

Next steps: Seaborn advanced visualizations Matplotlib advanced customizations

slide-51
SLIDE 51

INTRODUCTION TO SEABORN

Next steps: Gather data

Next steps: Python SQL

slide-52
SLIDE 52

INTRODUCTION TO SEABORN

Next steps: Transform and clean

Next steps: Getting data into Pandas DataFrames Cleaning data Transforming into tidy format

slide-53
SLIDE 53

INTRODUCTION TO SEABORN

Next steps: Analyze and build models

Next steps: Statistical analysis Calculating and interpreting condence intervals

slide-54
SLIDE 54

Congratulations!

IN TRODUCTION TO S EABORN