How to align y-axis tick labels left justified in matplotlib?
The 1st answer below gives renderer error at this line: Matplotlib: Aligning y-ticks to the left
pad = max(T.label.get_window_extent().width for T in yax.majorTicks)
The 2nd answer below uses text which is input dependent, for each list of yticklabels you have to first find the best x position and then you can use. Therefore, it not a solution for automated code.
How do you align tick labels in matplotlib?
Here is the code to replicate the problem:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1)
ax.plot([0,.25,.5, 1], [0,1,2,3])
ticklabels = ax.get_xticklabels()
ax.set_yticks([0, 1, 2, 3])
yticklabels = []
for i in ['xxxxxxxx', 'yyy', 'zzzzzzzzzzzzz', 'ttttt']:
yticklabels.append(f"{i:<15}")
print(yticklabels)
ax.set_yticklabels(yticklabels )
plt.show()