【问题标题】:Python matplotlib decrease size of colorbar labelsPython matplotlib 减小颜色条标签的大小
【发布时间】:2013-03-09 00:36:12
【问题描述】:

我需要你的帮助!我有一个绘图代码,如下所示:

fig = plt.figure()
ax1 = fig.add_subplot(111)

imax1 = ax1.imshow(data,interpolation = 'nearest', origin = 'lower',cmap=cm.jet)#plot
cbar = plt.colorbar(imax1, extend='neither', spacing='proportional',
                orientation='vertical', shrink=0.7, format="%.0f")
cbar.set_label(r"ET [mm/month]", size=10)

titlestr = "Evapotranspiration in mm/month"
plt.title(titlestr)
#plt.xlabel("Longitude")
#plt.ylabel("Latitude")
imax1.set_clim(0,60)
labels = [item.get_text() for item in ax1.get_xticklabels()]
for ii in range(np.shape(labels)[0]):
    labels[ii] = str(grid_lon[75*ii/np.shape(labels)[0]])

ax1.set_xticklabels(labels, rotation = 45, ha='right', size = 10)

labels = [item.get_text() for item in ax1.get_yticklabels()]
for ii in range(np.shape(labels)[0]):
    labels[ii] = str(grid_lat[75*ii/np.shape(labels)[0]])

ax1.set_yticklabels(labels, size = 10)

pngname = "./out/2d_"+variable+"_"+mm+".png"
print "save ", pngname
plt.savefig(pngname, dpi=None, facecolor='w', edgecolor='w',
    orientation='portrait', papertype=None, format=None,
    transparent=False, bbox_inches=None, pad_inches=0.1)


print "plot finished"

我想将颜色条标签的标签大小(例如 0、10、20、...60)设置为 10 或更小。这可能会进入“imax1.set_clim(0,60) 行。有什么想法吗?我也有兴趣将 imax1 对象的信息打印到命令行。我该怎么做?例如 imax1 的可用属性和功能.

非常感谢您的帮助!

【问题讨论】:

  • 部分回答:print dir(imax1) 将为您提供有关imax1 的可用属性和功能的信息。
  • 非常感谢,我实现了较低的。

标签: python attributes matplotlib labels colorbar


【解决方案1】:

啊哈!找到答案here

cbar.ax.tick_params(labelsize=10) 

附:点赞that回答并给保罗一些爱!

【讨论】:

  • 注意:cbar.ax.tick_params(labelsize='xx-small') 也可以使用
  • 有没有办法通过 rcParams 做到这一点?
  • 工作就像一个魅力
  • 注2:这不会改变颜色条标签的字体大小;另外,还需要运行:cbar.set_label('my label', size='xx-small')
【解决方案2】:

如果你在这里是因为你想增加字体大小,但上面的答案有些数字因为大号而消失了,你可以这样做

cbar = plt.colorbar()
for t in cbar.ax.get_yticklabels():
     t.set_fontsize(20)

【讨论】:

    猜你喜欢
    • 2013-05-18
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多