Completing Our First Plot

We can now complete our understanding of this code block we first saw in the Our First Plot lesson.

We went through the first four lines in the Libraries and Packages lesson. And now, with our understanding of functions, from the previous lesson, we can delve into the last line, which introduces the sns.relplot function.

In this line, we can identify four parameters in the sns.relplot function: data, x, y and hue. Let's go through these in some detail.

sns.relplot

We call the relplot function from the Seaborn library, which we load in with the alias sns. Seaborn is a data visualization package that is built on top of the matplotlib package and makes it easier to make attractive visualizations. And the relplot function in this library can make scatterplots, which is what we are doing here.

data

The data parameter, is the first positional parameter in the function. It expects a dataframe, and in this case, we provide the iris dataframe. Here are three observations from this dataset:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
7 3.2 4.7 1.4 versicolor
6.3 3.3 6 2.5 virginica

x and y

The second and third parameters in the function are x and y. Writing x = 'Sepal.Length' and y = 'Petal.Length' directs sns.relplot function to place the Sepal.Length data on the x-axis and the Petal.Length data on the y-axis.

hue

The fourth parameter passed into the function is the hue parameter. This optional parameter is used to colour the points based on groups within the argument we pass to it, in this case the Species variable. Adding colour to a plot really makes it pop!

Conclusion

Hidden from us within sns.relplot is code that will know how to take our arguments to produce a scatter plot with labelled axes and points representing our data. There are more plots we will explore in future lessons such as the line plot, bar plot, and violin plot.

This marks the final lesson in the Introduction section. In the next lesson we will transition to exploring fundamental concepts of Python and broader computer science. But before we move on, let's admire that beautiful plot once more!

Plot made from an interactive Python code editor