0

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()

enter image description here

burcak
  • 1,009
  • 10
  • 34
  • Depending on your backend, for the first solution you might need to call `fig.canvas.draw()` instead of `plt.draw()`. (Or `plt.pause(0.1)`.) – JohanC Jul 03 '21 at 10:53
  • I didn't set any backend. – burcak Jul 03 '21 at 10:57
  • 1
    You also have a backend. If you don't set it, you get the default one depending on the environment where you are running your code. `import matplotlib; print(matplotlib.get_backend())` to see the one you're currently using – JohanC Jul 03 '21 at 10:58
  • My backend is MacOSX. How to left align the y axis ticklabels? why is this this much difficult? – burcak Jul 03 '21 at 11:25
  • Did you try to replace `plt.draw()` with `fig.canvas.draw()`? Did you get an error? And with `plt.draw()` followed by `plt.pause(0.1)`? It's not that hard to test and [complete](https://stackoverflow.com/posts/68234707/edit) your question with all relevant information. You might want to reread [How to ask?](https://stackoverflow.com/help/how-to-ask). Things are a bit complicated, because exact measures can only be gotten when the plot is really drawn; changing parameters changes the plot and the measurements; for speed, matplotlib tries only to render when explicitly needed. – JohanC Jul 03 '21 at 11:47
  • If you want spaces and all characters to have the same width, you can use `ax.set_yticklabels(yticklabels, family='consolas')`. This changes the font to a monospaced one, which usually doesn't look as nice as the standard fonts. – JohanC Jul 03 '21 at 11:58
  • @JohanC fig.canvas.draw() didn't change anything. plt.draw() followed by plt.pause(0.1) just shows numbers at the y axis tick labels instead of given text. For your second recommendation, font and how it looks matter in a figure, that's why I'm trying to left justify. Thanks but I couldn't solve this issue yet. – burcak Jul 03 '21 at 13:26
  • If you only see numbers, that means you forgot to call `ax.set_yticklabels(yticklabels)`? Please [edit](https://stackoverflow.com/posts/68234707/edit) your question and add all relevant information, especially the exact code you tried last. – JohanC Jul 03 '21 at 14:15
  • 1
    Left justifying a piece of text based on the width of another piece of text is indeed hard. Yaxis ticks are right-justified for a reason. If you want them left justified, you will have to jump through hoops. – Jody Klymak Jul 03 '21 at 14:45
  • @JodyKlymak I see. – burcak Jul 03 '21 at 23:23

0 Answers0