I have the following code using the seaborn
library in python that plots a grid of histograms from data from within the seaborn
library:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns, numpy as np
from pylab import *
penguins = sns.load_dataset('penguins')
sns.displot(penguins, x='bill_length_mm', col='species', row='island', hue='island',height=3,
aspect=2,facet_kws=dict(margin_titles=True, sharex=False, sharey=False),kind='hist', palette='viridis')
plt.show()
This produces the following grid of histograms:
And so we have histograms for each species-island combination showing the frequency distribution of different penguin bill lengths, organized in a "grid" of histograms, where the columns of this grid of histograms are organized by species and the rows of this grid are organized by island. And so, I see that seaborn automatically names each column label as the "species" by the argument: col=species
. I then see seaborn labels each row as "Count" with the rows organized by island, with different representative "hues" from the argument: hue=island
.
What I am trying to do is override these default automatic labels to add my own customization. Specifically what I want to do is replace the top axes labels with just "A", "B", and "C" below a "Species" header, and on the left axis, replace each "Count" instance with the names of each island, but all of these labels in much bigger font size.
This is what I am trying to produce:
What I am trying to figure out is, how can I "override" the automatic labelling from the above seaborn arguments so that I can print my custom histogram grid labels, but done in a dynamic way, such that if there were potentially another data set with more islands and more species, the intended labelling organization would still be produced?
The sns.displot
function returns a FacetGrid
object. This object let you customize the row and col titles with the methods set_titles()
set_axis_labels()
. However, with the very custom figure you want to achieve, I'm afraid you'll have to overwrite labels and titles directly through FacetGrid.axes
which gives you access to a ndarray
of matplotlib.Axis
.
g = sns.displot(penguins, x='bill_length_mm', col='species', row='island', hue='island',height=3,
aspect=2,facet_kws=dict(margin_titles=True, sharex=False, sharey=False), kind='hist', palette='viridis',
legend=False) # Do not display the legend
g.set_titles(row_template="") # Remove the marginal titles on the right side
# Rewrite the top-row axis titles
custom_colnames = ["A", "B", "C"]
for i, ax in enumerate(g.axes[0]):
ax.set_title(custom_colnames[i], fontsize=14) # Adjust the fontsize
# Rewrite the first-col axis ylabels
custom_rownames = penguins["species"].unique()
for i, ax in enumerate(g.axes[:, 0]):
ax.set_ylabel(custom_rownames[i], fontsize=14, rotation=0, ha="right")
# Remove the last-row axis xlabels
for i, ax in enumerate(g.axes[-1]):
ax.set_xlabel("")
# Add a figure suptitle
plt.gcf().suptitle("Species", y=1.05, fontsize=16)
Very custom, but it should give you the desired figure