Introduction

I’m learning data visualization in Python and I see myself as a ‘hands on’ learner, so I’ll be reproducing some basic plots using seaborn package that you can use as a reference everytime you need to fresh up your memory.

At first is required that the packages are properly imported, after that I load the iris dataset.

1
2
3
4
5
6
7
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

url = "https://git.io/JXciW"

iris = pd.read_csv(url)

If you’re not familiar with the iris dataset, you can see its first five rows below:

sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa

Barplots

To create simple barplots.

1
sns.barplot(x="species", y="petal_width", data=iris)

seaborn barplot species x petal_width

Making a horizontal barplot.

1
sns.barplot(x="petal_width", y="species", data=iris)

seaborn barplot horizontal species x petal_width

Custom bar order.

1
2
3
4
5
sns.barplot(
    x="species",
    y="petal_width",
    data=iris,
    order=["virginica", "setosa", "versicolor"])

seaborn barplot custom bar order

Add caps to error bars.

1
sns.barplot(x="species", y="petal_width", data=iris, capsize=.2)

seaborn barplot caps error

Barplot withough error bar.

1
sns.barplot(x="species", y="petal_width", data=iris, ci=None)

barplot no error bar

Scatterplots

A simple scatterplot.

1
sns.scatterplot(x="sepal_width", y="petal_width", data=iris)

seaborn scatterplot

Mapping groups to scatterplot.

1
sns.scatterplot(x="sepal_width", y="petal_width", data=iris, hue="species")

seaborn scatterplot grouped

Mapping groups and scalling scatterplot.

1
2
3
4
5
6
sns.scatterplot(
    x="sepal_width",
    y="petal_width",
    data=iris,
    hue="sepal_length",
    size="sepal_length")

seaborn scatterplot grouped size

Legend and Axes

To change the plot legend to the outside of the plot area, you can use bbox_to_anchor = (1,1), loc=2. The following plot has a custom title, a new x axis label, and a y axis label.

1
2
3
4
5
6
7
8
sns.scatterplot(x="sepal_width", y="petal_width", data=iris, hue="species")
plt.legend(
    title="Species",
    bbox_to_anchor = (1,1),
    loc=2)
plt.xlabel("Sepal Width")
plt.ylabel("Petal Width")
plt.title("Sepal Width x Petal Width")

seaborn scatterplot outside legend with custom title and axis labels