【问题标题】:Bold ticklabels colorbar matplolib粗体刻度标签颜色条 matplotlib
【发布时间】:2020-02-18 17:28:25
【问题描述】:

为了使用 cartopy 在子图中保持颜色条方面,我在我的图中插入了新轴 (add_axes),但另外,我需要更改刻度标签颜色条和粗体的大小。但是 tick_params 和 set_yticklabels 在新轴上不起作用。

# pos x,  pos y, size x, size y
cb_ax = fig.add_axes([0.91, 0.302, 0.015, 0.383])

bar = fig.colorbar(img, cax=cb_ax, extend='max',
                   shrink=0.8, pad=0.0, spacing='uniform',
                   orientation='vertical', ticks=clevs,
                   extendfrac='auto')

bar.set_label(label=f'(mm)', size=10, weight='bold')

# not working
# bar.tick_params(labelsize=10)

# not working
# bar.img.set_yticklabels(clevs, fontsize=9, weight='bold')

完整代码:https://pastebin.com/NfiMWf2n

绘图结果:https://1drv.ms/u/s!Amb6LUmV4LnKi55gRf6DqGDKjfTGxA?e=HCGeZb

【问题讨论】:

    标签: python matplotlib colorbar cartopy


    【解决方案1】:

    可以设置bar.ax的粗细和字体大小:

    import matplotlib.pyplot as plt
    import numpy as np
    fig=plt.figure()
    img = plt.imshow(np.random.random((4,4)))
    cb_ax = fig.add_axes([0.91, 0.302, 0.015, 0.383])
    bar = fig.colorbar(img, cax=cb_ax, extend='max',
                       shrink=0.8, pad=0.0, spacing='uniform',
                       orientation='vertical',
                       extendfrac='auto')
    fig.canvas.flush_events() #else bar.ax.get_yticklabels() is not yet updated
    bar.ax.set_yticklabels(labels=bar.ax.get_yticklabels(), weight='bold', fontsize=5)
    bar.set_label(label=f'(mm)', size=10, weight='bold')
    

    输出:

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 2020-05-18
      • 1970-01-01
      • 2013-09-13
      相关资源
      最近更新 更多