Search code examples
pythonmatplotlibseaborn

How to align y labels in a Seaborn PairGrid


Taken from the documentation:

>>> import matplotlib.pyplot as plt
>>> import seaborn as sns; sns.set()
>>> iris = sns.load_dataset("iris")
>>> g = sns.PairGrid(iris)
>>> g = g.map(plt.scatter)

PairGrid

How can I align y labels?

What I tried:

for ax in g.axes.flat:
    ax.yaxis.labelpad = 20

However, that only shifts all labels by the specified amount instead of aligning them.


Solution

  • Yeah, that alignment is terrible. How dare a package that boasts to make "attractive statistical graphics" fail so abysmally at the alignment of labels?! Just kidding, I love me some seaborn, at least for inspiration on how to improve my own plots.

    You are looking for:

    ax.get_yaxis().set_label_coords(x,y) # conveniently in axis coordinates
    

    Also, I would only iterate over the axes that show the label, but that is just me:

    for ax in g.axes[:,0]:
        ax.get_yaxis().set_label_coords(-0.2,0.5)
    

    0.5 for a centred vertical alignment, -0.2 for a slight offset to the left (trial-and-error).

    enter image description here