Multivariate views#
In this notebook, we show a few examples of how to have plots with graphs of different types in a figure, like having a scatter plot with marginal distributions or even a multivariate plot with pair relationships of all properties in a table.
import pandas as pd
import seaborn as sns
sns.set_theme()
Let’s load the same dataframe.
df = pd.read_csv("../data/BBBC007_analysis.csv")
df.head()
Plotting joint and marginal distributions#
To have a joint distribution of two variables with the marginal distributions on the sides, we can use jointplot
.
sns.jointplot(data=df,
x="aspect_ratio",
y="area");
As expected, it is possible to separate groups by passing a categorical property to the hue
argument. This has an effect on the marginal distribution, turning them from histogram to kde plots.
sns.jointplot(data=df,
x="aspect_ratio",
y="area",
hue='file_name');
Plotting many distributions at once#
The above examples displayed a plot with relationship between two variables. This can be further expanded with the pairplot
function which displays the relationship between all variables in a table. The result is a matrix of scatter plots with an univariate distribution of each variable on the diagonal.
sns.pairplot(data=df);
As in the former examples, we can separate the groups by passing a categorical property to the hue
argument.
sns.pairplot(data=df,
hue="file_name");
If you have too many points, displaying every single point may yield graphs too poluted. An alternative visualization in this case could be a 2D histogram plot. We can do that by changing the kind
argument to “hist”. With this, each subplot describes the bivariate relationships between different pairs of variables. Essentially, it offers a heat-map-like view, where the intersection of intervals between two variables shows the density of data points.
sns.pairplot(data=df,
hue="file_name",
kind="hist");
Exercise 4#
You may have noticed that the pairplot
is redundant in some plots because the upper diagonal displays the same relationships rotated.
Redraw the pairplot
to display only the lower diagonal of the plots.
Hint: explore the properties of the pairplot
.
# Your code here