Seaborn Plot Functions Hierarchy

One function for each family of plots

Balaji Sundararaman
Towards Data Science

--

Photo by Evan Walmsley on Unsplash

The seaborn package in python is the go-to for most of our tasks involving visual exploration of data and extracting insights. Based on matplotlib, seaborn enables us to generate cleaner plots with a greater focus on the aesthetics.

Now with version 0.11.0 released this month, seaborn is not just a collection of plotting functions, with one function each for a particular type of plot.

There is a clearly defined hierarchy and grouping of the plotting functions based on the nature of task at hand:

  • Univariate Analysis (Distribution) : displot()
  • Relational Analysis ( Interaction & Relationship between features) : relplot()
  • Analysis of Categorical Features: catplot()

These 3 top level functions , also called Figure-Level functions, can be used for all plots under the main categories.

Source: Seaborn Documentation https://seaborn.pydata.org/index.html

Why should we care about this? Well, there are some benefits to understanding this finer nuance about how seaborn goes about doing its job. These benefits are listed below:

  • You need to know only the 3 functions — relplot(), displot(), catplot()instead of the dozen plotting functions overall.
  • The figure-level functions provide an identical interface/signature for all the plots in their respective category.
  • By default, this ensures that the legend, if applicable, is plotted outside the plot reducing interference with the main plot elements
  • Easy to generate facet plots based on a feature of the dataset, compared to matplotlib, and the code is easier on the eye as well
  • Easier figure level customization

Lets check out these aspects. We will use the built-in tips dataset in seaborn.

Univariate Analysis — Distribution

The displot function (you read it right! it is not a typo.. it is displot and not distplot which has now been deprecated) caters to the three types of plots which depict the distribution of a feature — histograms, density plots and cumulative distribution plots. Note that, we are calling only displot in all 3 cases and just changing the kind argument to get a different plot.

Bi/Multi Variate Analysis — Relational Plots

The relplot function caters to the family of relational plots (which bring out the relationship/interaction or the lack of it) between 2 or more features — scatterplots and lineplots.

For the lineplot lets assume we want to check the trend of the average bill amount by day of the week. We use the groupby() to group the observations by day feature in the dataset. Then, we plot the lineplot using the relplot. We can see that there is a distinct increase in the mean bill value at the restaurant on the weekends compared to the weekdays.

Analysis of Categorical Features

The figure-level function catplot caters to all the categorical feature plots

Facet Plots

Facet plotting is a breeze with seaborn and the figure-level functions mentioned above make it even easier with far less code and clutter compared to matplotlib. Check out the plots below:

The columns can be wrapped by adding the col_wrap argument and passing an integer value.

If you are a creature of habit, you can still continue to use the individual plotting functions under the figure level plotting functions shown at the beginning of the article.

Summary & Conclusion

The common figure level functions in seaborn 0.11.0 release simplify the plotting function interfaces and provides us with a common function signature for all plots under each category of plots. This also reduces the code and clutter to achieve the same results through matplotlib. Facet plots/ subplots are also easily achieved with a single line of code.

You may also like the below articles on ways to simplify data visualization & exploratory data analysis:

Thank you for reading !

--

--

Passionate about Data Analytics, Visualization and Machine Learning with extensive experience across functions in India’s emerging Fintech vertical