Plotting with seaborn

10.3. Plotting with seaborn#

Let’s next explore how to simply make plots out of pandas DataFrames data using seaborn. To start working with the seaborn library, we use the following import line:

import seaborn as sns

Let’s look at an example with our penguins dataset.

import pandas as pd

penguins = pd.read_csv("penguins.csv")
penguins.head(5)
../../_images/df-head.png
import seaborn as sns

# Scatter plot with seaborn
sns.scatterplot(data=penguins, x="Body Mass (g)", y="Flipper Length (mm)")
../../_images/fig1.png

Notice how all selections happen within sns.scatterplot function - you can define your pandas DataFrame as data, and pass column names of your DataFrame as x and y. Also, the labels of x- and y-axes are added automatically, which is quite convenient!

In this case, we selected two columns with numerical values, but it would also work with a non-numeric column, e.g., Clutch Completion.

sns.scatterplot(data=penguins, x="Clutch Completion", y="Flipper Length (mm)")
../../_images/fig2.png

If you wanted to make a more fine-grained distinction in your plot, e.g., color data points by values in another column, that’s also possible. Let’s say you’re interested which data points in the Body mass (g) vs. Flipper length (mm) plot belong to male or female penguins. You can use hue for that - seaborn will then give different colors base on the values in the column Sex. Conveniently, it also creates a legend for you and tries to find an optimal position for it.

sns.scatterplot(data=penguins, x="Body Mass (g)", y="Flipper Length (mm)", hue="Sex")
../../_images/fig3.png

If you wish to add even more data, e.g., also account for Species, you can do that by assigning different symbols (markers) based on the values in the Species column. We can easily do that with the style argument:

sns.scatterplot(data=penguins, x="Body Mass (g)", y="Flipper Length (mm)", hue="Sex", style="Species")
../../_images/fig4.png

We notice that the position of the legend is now overlapping with our data, so let’s move it to the side:

ax = sns.scatterplot(data=penguins, x="Body Mass (g)", y="Flipper Length (mm)", hue="Sex", style="Species")
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
../../_images/fig5.png

If you now wanted to save this figure, you could use this code:

ax.figure.savefig("Figure_name.png", dpi=600)

where dpi makes sure your figure is of high quality. If the figure is quite wide (like the one where we move the legend to the side), you can also add bbox_inches = "tight" to make sure it doesn’t get cut out.

Tip

In our examples, we used sns.scatterplot. As you can imagine, seaborn offers many more plot types, which you can easily access with, e.g., sns.barplot, sns.boxplot, etc.