【问题标题】:Offset tick labels in colorbar颜色栏中的偏移刻度标签
【发布时间】:2019-05-09 15:16:57
【问题描述】:

考虑下图(生成它的代码如下):

上部刻度标签 (9.5E-05) 看起来不错,但我希望底部和中间刻度标签垂直向上对齐(注意它不是应用于两者的相同对齐方式)所以他们看起来像这样:

看起来好多了(至少对我来说)。我一直在环顾四周,但没有找到方法。


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable


ax = plt.subplot(111)
data = np.random.uniform(0., 10., (50, 50))
im = plt.imshow(data)

# Colorbar on the right side of ax.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2%", pad=0.05)
cbar = plt.colorbar(im, cax=cax)
cbar.set_ticks([np.min(data), np.ptp(data) * .5, np.max(data)])
print(np.min(data), np.ptp(data) * .5, np.max(data))

cbar.ax.set_yticklabels([
    '{:.1E}'.format(0.00000133),
    '{:.1E}'.format(0.00000561),
    '{:.1E}'.format(0.0000948)], rotation=90)

plt.show()

【问题讨论】:

    标签: python matplotlib colorbar


    【解决方案1】:

    您可以循环浏览颜色条的 y 刻度标签,并根据它是哪个标签设置文本的垂直对齐方式。

    # don't include last label (one at the top) as we don't want to change its alignment
    for i, label in enumerate(cbar.ax.get_yticklabels()[:-1]):
        if i == 0:  # first label
            label.set_va("bottom")
        else:
            label.set_va("center")
    

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 1970-01-01
      • 2021-03-15
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 2013-09-27
      相关资源
      最近更新 更多