Home > Back-end >  Aligning y-axis label with middle of subplot?
Aligning y-axis label with middle of subplot?

Time:01-18

I'm working with Matplotlib and have a large number of 1D heatmaps, each with their own label. However, the labels are misaligned with the plots and I cannot figure out to get this to work automatically.

Here's an MWE

import numpy as np
import matplotlib.pyplot as plt


data = np.random.rand(10, 1000)
dogs = ["woof", "bark", "bowwow"]
fig, axs = plt.subplots(10)
for i in range(10):
    axs[i].scatter(np.linspace(0, 1, 1000), np.linspace(0,1,1000)*0, 2000,
                     c=data[i, :], marker="|", cmap='inferno')
    axs[i].set_frame_on(False)
    axs[i].set_yticklabels([])
    axs[i].set_xticklabels([])
    axs[i].set_xticks([])
    axs[i].set_yticks([])
    axs[i].set_ylabel(dogs[i%3], rotation='horizontal')
plt.show()

I experimented with

    axs[i].yaxis.set_label_coords(x, y)

for various values of x and y, and nothing seems to work. I would prefer to have it align automatically, with the bottom of the text corresponding to the bottom of the individual plot.

Attached is an image showcasing the alignment issue.

vertical grid of heatmaps with labels

CodePudding user response:

After a bit of playing around, I found that

axs[0].set_ylabel("Pseudotime", fontsize=12, rotation='horizontal', ha='right', va='center')

is sufficient for aligning the y-labels.

  • Related