I have a collection of binned data from which I generate a series of seaborn pairplots. Since all of the bins have the same labels, but not bin names, I need to annotate the pairplots with the bin name 'n' below so that I can later associate them with their bins.
import seaborn as sns
groups = data.groupby(pd.cut(data['Lat'], bins))
for n,g in groups:
p = sns.pairplot(data=g, hue="Label", palette="Set2",
diag_kind="kde", size=4, vars=labels)
I noted in the documentation that seaborn uses, or is built upon, matplotlib. I have been unable to figure out how to annotate the legend on the left, or provide a title above or below the paired plots. Can anyone provide examples of pointers to the documentation on how to add arbitrary text to those three areas of a plot?
The Seaborn Pairplot allows us to plot pairwise relationships between variables within a dataset. This creates a nice visualisation and helps us understand the data by summarising a large amount of data in a single figure. This is essential when we are exploring our dataset and trying to become familiar with it.
pairplot() : To plot multiple pairwise bivariate distributions in a dataset, you can use the . pairplot() function. The diagonal plots are the univariate plots, and this displays the relationship for the (n, 2) combination of variables in a DataFrame as a matrix of plots.
To show the title for the diagram for Seaborn pairplot(), we can use pp. fig. suptitle() method.
After following up on mwaskom's suggestion to use matplotlib.text()
(thanks), I was able to get the following to work as expected:
p = sns.pairplot(data=g, hue="Label", palette="Set2",
diag_kind="kde", size=4, vars=labels)
#bottom labels
p.fig.text(0.33, -0.01, "Bin: %s"%(n), ha ='left', fontsize = 15)
p.fig.text(0.33, -0.04, "Num Points: %d"%(len(g)), ha ='left', fontsize = 15)
and other useful functionality:
# title on top center of subplot
p.fig.suptitle('this is the figure title', verticalalignment='top', fontsize=20)
# title above plot
p.fig.text(0.33, 1.02,'Above the plot', fontsize=20)
# left and right of plot
p.fig.text(0, 1,'Left the plot', fontsize=20, rotation=90)
p.fig.text(1.02, 1,'Right the plot', fontsize=20, rotation=270)
# an example of a multi-line footnote
p.fig.text(0.1, -0.08,
'Some multiline\n'
'footnote...',
fontsize=10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With