Consider a simple plot with two distinct populations drawn:

I want to add two separate y labels, one for the top population and one for the one below on the same left y axis. All the similar questions I've seen deal with labels on two separate axis (ie: one on the left, one on the right) which is not what I need.
How can I do this?
import numpy as np
import matplotlib.pyplot as plt
N = 50
a = np.random.uniform(100., 120., N)
b = np.random.uniform(10., 20., N)
plt.scatter(range(N), a)
plt.scatter(range(N), b)
plt.show()
This question is not about adding labels to a legend as described here.
You may misuse the minor ticks for the y axis to place labels at those positions. Here we may take the mean of the data to create a minor tick. The tick can be turned off and the label can be padded, rotated and aligned such that it looks similar to a usual ylabel.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
N = 50
a = np.random.uniform(100., 120., N)
b = np.random.uniform(10., 20., N)
plt.scatter(range(N), a)
plt.scatter(range(N), b)
ax = plt.gca()
ax.yaxis.set_minor_locator(mticker.FixedLocator((a.mean(), b.mean())))
ax.yaxis.set_minor_formatter(mticker.FixedFormatter(("Label A", "Label B")))
plt.setp(ax.yaxis.get_minorticklabels(), rotation=90, size=15, va="center")
ax.tick_params("y",which="minor",pad=25, left=False)
plt.show()

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