Changing plot style and color

Introduction to Data Visualization with Seaborn

Erin Case

Data Scientist

Why customize?

Reasons to change style:

  • Personal preference
  • Improve readability
  • Guide interpretation
Introduction to Data Visualization with Seaborn

Changing the figure style

  • Figure "style" includes background and axes
  • Preset options: "white", "dark", "whitegrid", "darkgrid", "ticks"
  • sns.set_style()
Introduction to Data Visualization with Seaborn

Default figure style ("white")

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

plt.show()

Point plot of masculinity survey

Introduction to Data Visualization with 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()

Point plot with white grid background

Introduction to Data Visualization with Seaborn

Other styles

sns.set_style("ticks")

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

plt.show()

Point plot with ticks

Introduction to Data Visualization with Seaborn

Other styles

sns.set_style("dark")

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

plt.show()

Point plot with dark background

Introduction to Data Visualization with Seaborn

Other styles

sns.set_style("darkgrid")

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

plt.show()

Point plot with dark grid background

Introduction to Data Visualization with 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
Introduction to Data Visualization with Seaborn

Diverging palettes

Four example diverging palettes

Introduction to Data Visualization with 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",
            order=category_order)

plt.show()

Count plot of survey responses

Introduction to Data Visualization with 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",
            order=category_order)

plt.show()

Count plot with diverging palette

Introduction to Data Visualization with Seaborn

Sequential palettes

Four example sequential palettes

Introduction to Data Visualization with Seaborn

Sequential palette example

Scatter plot of horsepower vs. mpg with sequential palette

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
Introduction to Data Visualization with Seaborn

Custom palettes

custom_palette = ["red", "green", "orange", "blue",
                  "yellow", "purple"]

sns.set_palette(custom_palette)

Custom color name palette

Introduction to Data Visualization with Seaborn

Custom palettes

custom_palette = ['#FBB4AE', '#B3CDE3', '#CCEBC5', 
                  '#DECBE4', '#FED9A6', '#FFFFCC', 
                  '#E5D8BD', '#FDDAEC', '#F2F2F2']

sns.set_palette(custom_palette)

Custom hex code palette

Introduction to Data Visualization with 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"
Introduction to Data Visualization with Seaborn

Default context: "paper"

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

plt.show()

Point plot with notebook context

Introduction to Data Visualization with 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()

Point plot with larger context

Introduction to Data Visualization with Seaborn

Let's practice!

Introduction to Data Visualization with Seaborn

Preparing Video For Download...