Search⌘ K
AI Features

Multi-Plot Grids with Different Styles

Discover how to use Seaborn alongside Matplotlib's subplot functionality to create complex multi-plot grids. Learn to position and style different plot types such as scatter, histograms, KDE, and violin plots within a single figure to visualize diverse data relationships effectively.

Overview

The seaborn library provides many functions for individual plots, such as scatter plots, KDE plots, hist plots, and so on. In addition, the faceting functionality is also available with plots such as relplot, displot, and more. However, our visualization may sometimes require a complex structure that may not be achieved using individual plot elements. For instance, let’s say we have to present the annual sales report of a firm. We’re interested in visualizing purchasing trends over time with a line plot and the number of units sold for each product with the help of a count plot. We might also want to see the relationship between product price and purchase history, which kind of products customers tend to buy, and so on. We can use the Matplotlib library’s subplots() function with seaborn plots to make multiple plots, with each one containing its own individual style and type of plot.

Matplotlib subplots with seaborn plots

We draw a 2x2 subplot using the plt.subplots() function to get started, as shown in line 3 of the code below. The constrained_layout=True parameter makes the figures and subplots visually better and ensures that no plot elements override each other.

Python
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(ncols=2, nrows=2, figsize=(8,8), constrained_layout=True)
plt.savefig('output/graph.png') # to save figure

Now, let’s draw a seaborn plot in our first grid. We use the sns.scatterplot() function to draw a scatter plot categorized by different penguin species. We pass the ax=axs[0,0] in the sns.scatterplot() function, which tells the function to draw the scatter plot at the location (0,0), which is the first grid, as shown in lines 2–3 of the code below:

Python
f, axs = plt.subplots(2, 2, figsize=(8, 8), constrained_layout=True)
sns.scatterplot(data=peng_df, x="flipper_length_mm",
y="bill_length_mm", hue="species", ax=axs[0,0],legend=False)
plt.savefig('output/graph.png')

Let’s plot the second seaborn plot in the second diagonal of our grid. We plot a hist plot in the second diagonal using the sns.histplot() function. In the sns.histplot() function call, we specify ax=axs[1,1], which draws the histogram at the location (1,1) of the grid, as shown in lines 5–6 of the code below:

Python
f, axs = plt.subplots(2, 2, figsize=(8, 8), constrained_layout=True)
# define plots
sns.scatterplot(data=peng_df, x="flipper_length_mm",
y="bill_length_mm", hue="species", ax=axs[0,0],legend=False)
sns.histplot(data=peng_df, x="species",
hue="species", shrink=.8, alpha=.8, legend=False, ax=axs[1,1])
plt.savefig('output/graph.png')

Next, we plot the third seaborn plot at location (0,1) of the grid. We draw a KDE plot using the sns.kdeplot() function, as shown in lines 7–8, along with the styling properties applicable to the KDE plot. Finally, we plot our last figure on the grid, as shown in line 9, which is the violin plot using the sns.violinplot() function at the (1,0) grid location.

Python
f, axs = plt.subplots(2, 2, figsize=(8, 8), constrained_layout=True)
# define plots
sns.scatterplot(data=peng_df, x="flipper_length_mm",
y="bill_length_mm", hue="species", ax=axs[0,0],legend=False)
sns.histplot(data=peng_df, x="species", hue="species",
shrink=.8, alpha=.8, legend=False, ax=axs[1,1])
sns.kdeplot(data=peng_df, x="body_mass_g",
hue="species", fill=True, lw =2, ax=axs[0,1])
sns.violinplot(data=peng_df, x="body_mass_g", ax=axs[1,0])
plt.savefig('output/graph.png')

We can merge individual seaborn plots and elements with Matplotlib subplots to produce complex visualizations according to our requirements.