A Comparison of Seaborn and Altair | by Soner Yıldırım | Towards Data Science

Data visualization is a substantial part of data science. It helps to better understand the data by unveiling the relationships among variables. The underlying structure within a dataset can also be explored using well-designed data visualizations.
In this article, we will compare two popular data visualization libraries for Python: Seaborn and Altair.
The comparison will be based on creating 3 sets of visualizations using both libraries. The focus of comparison is on the syntax, the structure of figures, and how these libraries handle the relationships between variables.
We will use a marketing dataset to create the visualizations. Let’s start by importing the dependencies and reading the dataset into a Pandas dataframe.
import numpy as npimport pandas as pdimport seaborn as snsimport altair as altdf_marketing = pd.read_csv("/content/DirectMarketing.csv")df_marketing.head()
The first set of visualizations includes basic histogram plots.
In Seaborn, we pass the name of the dataframe and the name of the column to be plotted. The height and aspect parameters are used to modify the size of the plot. Aspect is the ratio of the width to height.
sns.displot(df_marketing, x='AmountSpent', aspect=1.5)
In Altair, we start with a top-level Chart object which takes the dataframe as argument. Then we specify the type of plot. The encode function takes the columns, relations, and transformations to be plotted. Anything we put in the encode function needs to be linked to the dataframe passed to the Chart. Finally, the properties function adjusts the size of the plot.
alt.Chart(df_marketing).mark_bar().encode(  alt.X('AmountSpent:Q', bin=True), y='count()').properties(height=300, width=450)
Altair is quite efficient at data transformations. Let’s explain the transformation done to create the above histogram.
alt.X('AmountSpent:Q', bin=True), y='count()'
What this line does is that it divides the amount spent column into bins and count the number of data points in each bin.
Both Seaborn and Altair provides simple ways to distinguish the data points of numerical variables based on different groups in categorical variables.
In Seaborn, this separation can be achieved by hue, col, or row parameters. Following code will return a scatter plot of the amount spent and salary columns and separation will be done according to gender.
sns.relplot(    data=df_marketing, x='AmountSpent', y='Salary',     kind='scatter', hue='Gender', aspect=1.5)
In Altair, we use the color parameter inside the encode function. It is similar to the hue parameter of Seaborn.
alt.Chart(df_marketing).mark_circle().encode(  x='AmountSpent', y='Salary', color='Gender').properties(height=300, width=450)
In some cases, it is more appealing or informative to use multiple plots in one visualization. Each subplot can be used to emphasize a certain feature or relationship so the overall visualization conveys more information.
There are many ways to create multi-plot visualizations. In Seaborn, we can use the pyplot interface of Matplotlib to manually create a grid by specifying the number of Axes objects in a Figure. Then we can generate a plot for each Axes object.
Another way is to use the FacetGrid or PairGrid to automatically generate a grid of plots. Based on the given variables (i.e. features), FacetGrid creates a grid of subplots which allows for transferring the structure of the dataset to the visualization. Row, col, and hue parameters can be considered as the three dimensions of FacetGrid objects.
g = sns.FacetGrid(   data=df_marketing, col='Gender', row='Location', hue='OwnHome',      height=4, aspect=1.4)g.map(sns.scatterplot, "Salary", "AmountSpent").add_legend()
We first construct the pattern using the hue, col, and row parameters. Hue parameter uses different colors for separation whereas col and row parameters use x axis and y axis, respectively. Then the map function is used to specify the type of plot along with the variables (i.e. columns in dataframe) to be plotted.
In Altair, the logic for creating such grids is quite similar to Seaborn.
alt.Chart(df_marketing).mark_circle().encode(  x='AmountSpent', y='Salary', color='OwnHome',  column='Gender', row='Location').properties(height=200, width=300)
In addition to the color parameter, we use the column and row parameters in the encode function to add dimensions to the returned visualization.
Both Seaborn and Altair are popular data visualization libraries for Python. Either one will be enough for most of the data visualizations tasks. There are small advantages of one over another in different aspects.
I think the syntax of Seaborn is a little simpler and easier to read than the syntax of Altair. On the other hand, Altair is superior to Seaborn with respect to data transformations. Altair also provides interactive visualizations.
Thank you for reading. Please let me know if you have any feedback.