在 matplotlib 颜色栏中对齐刻度标签 [英] Align ticklabels in matplotlib colorbar

查看:44
本文介绍了在 matplotlib 颜色栏中对齐刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有正负值的色条,它们是自动生成的(我没有设置它们).不幸的是,减号破坏了文本的垂直对齐方式.如何使刻度标签中的所有文本向右对齐,或者如何在我的正数之前插入空格以使其看起来不错?

解决方案

我刚想问同样的问题,终于找到了你的.所以我想添加自己的解决方法.

起初,我使用完全相同的解决方案

I have a colorbar with positive and negative values which are generated automatically (I am not setting them). Unfortunately the minus sign breaks the vertical alignment of the text. How can I align all the text in the ticklabels to the right or alternatively insert a space before my positive numbers to make it look good?

解决方案

I was just about to ask the same question, then finally found yours. So I'd like to add my own work-around.

At first I used the exact same solution given by Rutger Kassies. However, when plotting in ipython, the same amount of shift in x position will give wildly different results when viewing a small window, or setting the figure to full screen.

I've eventually settled on setting the pad of the colorbar's tick labels. Borrowing Blatantly stealing the sample code of Rutger:

a = np.random.randn(10,10)

fig, ax = plt.subplots()

im = ax.imshow(a, interpolation='none')
cb = plt.colorbar(im)

ticklabs = cb.ax.get_yticklabels()
cb.ax.set_yticklabels(ticklabs,ha='right')
cb.ax.yaxis.set_tick_params(pad=45)  # your number may vary

This workaround seems to give a much more consistent result when using across various window sizes and aspects.

Here's 4 outputs to demonstrate what I mean: top row is the position shifting solution, bottom row is my padding solution. On the left are the default figure window sizes, the right side was savefigged after popping the figure window to full screen:

这篇关于在 matplotlib 颜色栏中对齐刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆