The code below produces plots like this one:
I need to show only the tick labels in the y axis that are over the horizontal line. In this case, the labels [2,3,4,5]
would need to be hidden. I've tried using
ax.get_yticks()
ax.get_yticklabels()
to retrieve the ticks that are drawn, and from those select only the ones above the y_min
value to show. Neither command returns the actual tick labels drawn in the plot.
How can I do this?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
# Some random data
x = np.random.uniform(1, 20, 100)
y = np.array(list(np.random.uniform(1, 150, 97)) + [4, 7, 9])
y_min = np.random.uniform(4, 10)
ax = plt.subplot(111)
ax.scatter(x, y)
ax.hlines(y_min, xmin=min(x), xmax=max(x))
ax.set_xscale('log')
ax.set_yscale('log')
ax.yaxis.set_minor_formatter(FormatStrFormatter('%.0f'))
ax.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.show()