More Seaborn

In this and the next few lessons lesson we return to the seaborn package, which we explored back in the Completing Our First Plot lesson. In the remainder of this and the next two lessons we will learn more plotting techniques to level up our data visualization skills.

Scatter plot with sns.relplot

Let's review what we have already learned about plotting with sns.relplot.

In the following scatter plot, we set Sepal.Length on the x-axis, set Petal.Length on the y-axis, colour in the points based on their Species, and produce a scatter plot with the relplot function in the Seaborn library.

Python interactive coding assignment

Let's go over each component in the code block to recap what everything signifies.

  • relplot: Is a function in the seaborn package, called using the alias sns that draws the plot.
  • data = iris: Is the first argument in the replot function and defines the data that can be used in the plot.
  • x = 'Sepal.Length': Sets the Sepal.Length variable as the x-axis.
  • y = 'Petal.Length': Sets the Petal.Length variable as the y-axis.
  • hue = 'Species': Colours the points based on the Species column.

Plot Annotations

We may add a plot title and axis labels using functions from the matplotlib library. The Seaborn library was built on top of the matplotlib library (abbreviated plt when it was imported), and as such, the two can interact to make highly customized plots. Adding text to a plot is referred to as annotating it. We can customize the labels of the x-axis and y-axis by using the plt.xlabel and plt.ylabel functions. Then we may add a title using the plt.title function. We add an optional argument y = 0.95 to push the title down a bit so it doesn't overfill the image space.

Python interactive coding assignment