• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

Mean and SD plot with Seaborn objects

datavizpyr · February 17, 2023 ·

In this tutorial, we will learn how to make Mean and Standard Deviation plot using Seaborn’s object orient interface Seaborn objects. Mean and SD plot plot across the different groups in adataset can quickly reveal the trend in the data.

We will use gapminder dataset to make the mean and SD values plot using Seaborn objects. The seaborn.objects is a new way to make plots using Seaborn that follows grammar of graphics rules and available from Seaborn version 0.12.0. With Seaborn objects, we can build a plot layer by layer.

Let us get started by loading the libraries needed to make mean SD plot with Seaborn.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import seaborn.objects as so
sns.__version__

0.12.2

We use the gapminder data directly from datavizpyr’s github page.

p2data = "https://raw.githubusercontent.com/datavizpyr/data/master/gapminder-FiveYearData.csv"
gapminder = pd.read_csv(p2data)
gapminder.head()


country	year	pop	continent	lifeExp	gdpPercap
0	Afghanistan	1952	8425333.0	Asia	28.801	779.445314
1	Afghanistan	1957	9240934.0	Asia	30.332	820.853030
2	Afghanistan	1962	10267083.0	Asia	31.997	853.100710
3	Afghanistan	1967	11537966.0	Asia	34.020	836.197138
4	Afghanistan	1972	13079460.0	Asia	36.088	739.981106

Plotting SD errorbors with Seaborn objects

Here is the basic syntax of a plot made with a single layer.

Seaborn Objects Syntax
Seaborn Objects Syntax

In the example below we make a plot showing a range for each continent’s standard deviation for lifeExp. We use Seaborn objects’s Range() function and Est() function to show the range.

(
    so.Plot(gapminder, x='lifeExp', y="continent")
    .add(so.Range(),so.Est(errorbar="sd"))
)

With Seaborn objects, we can save a plot by using save() function as additional layer.

(
    so.Plot(gapminder, x='lifeExp', y="continent")
    .add(so.Range(),so.Est(errorbar="sd"))
    .save("sd_plot_seaborn_objects.png",
           format='png',
           dpi=150)
)

Plotting Mean values with Seaborn objects

we can plot the mean values per group on y-axis using Dot() and Agg() functions in Seaborn objects.

(
    so.Plot(gapminder, x='lifeExp', y="continent")
    .add(so.Dot(),so.Agg())
    .save("mean_plot_seaborn_objects.png",
           format='png',
           dpi=150)

)
Mean as dot using Seaborn objects
Mean as dot using Seaborn objects

Building the mean and SD plot layer by layer

Now that we have shown independently how to plot mean and the SD range, we can add as layers to the same plot with Seaborn Objects.

(
    so.Plot(gapminder, x='lifeExp', y="continent")
    .add(so.Dot(),so.Agg())
    .add(so.Range(),so.Est(errorbar="sd"))
    .save("mean_sd_plot_seaborn_objects.png",
           format='png',
           dpi=150)

)
Mean and SD plot Seaborn Objects
Mean and SD plot Seaborn Objects

Adding color by variable to mean and SD error plot

We can also customize the mean and SD plot by adding colors based on the values of a variable in the data. In the example below we color by continet.

(
    so.Plot(gapminder, x='lifeExp', y="continent", color='continent')
    .add(so.Dot(),so.Agg())
    .add(so.Range(),so.Est(errorbar="sd"))
    .save("mean_sd_plot_colored_seaborn_objects.png",
           format='png',
           dpi=150)

)
Mean and SD plot using Seaborn Objects
Mean and SD plot using Seaborn Objects

Vertical Mean and SD plot

In the above examples, we made horizontal mean and SD plot, to make it a vertical plot, we switch the and x and y axis variables as shown below.

(
    so.Plot(gapminder, y='lifeExp', x="continent", color='continent')
    .add(so.Dot(),so.Agg())
    .add(so.Range(),so.Est(errorbar="sd"))
    .save("mean_sd_plot_colored_vertical_seaborn_objects.png",
           format='png',
           dpi=150)

)
Mean and SD plot (vertical) with Seaborn Objects
Mean and SD plot (vertical) with Seaborn Objects

Related posts:

Seaborn Histogram with facet using Grammar of Graphics APIHistogram Tutorial with Seaborn Grammar of Graphics API Boxplot with Color Palette Set3 SeabornHow To Use Seaborn Color Palette to Color Boxplot Seaborn Scatterplot: Change edgecolor and line widthHow To Change Edge Color on Seaborn Scatter Plot? Combine Two plots into one in SeabornHow to Combine Two Seaborn plots with Shared y-axis

Filed Under: Python, Seaborn, Seaborn objects Tagged With: Mean and SD Plot Seaborn

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version