python colorbar设置label的方法

label:整个 colorbar 的标签,类似于 axes 的 xlabel 或 ylabel

这里介绍设置colorbar的label的两种方法,可以在官网查看其他属性:

Standalone colorbars — Matplotlib 3.8.2 documentationicon-default.png?t=N7T8https://matplotlib.org/stable/users/explain/colors/colorbar_only.html

1. 比较简单的加个label,可以直接在colorbar属性中添加

import matplotlib as mpl
import matplotlib.pyplot as plt

cb = fig.colorbar(
         image, orientation="horizontal", 
         fraction=0.045,
         label='a colorbar label')

2. 通过set_label设置

cb = fig.colorbar(
         image, orientation="horizontal", fraction=0.045
)

cb.set_label('sea ice concentration (%)', fontsize=12, loc='center')

通过set_label的loc参数能设置colorbar的位置

For horizontal orientation one of {‘left’, ‘center’, ‘right’}
For vertical orientation one of {‘bottom’, ‘center’, ‘top’}

参考:python colorbar设置label标签位置_pythonlabel位置-CSDN博客

3.  通过设置ax,对ax添加title

cb = fig.colorbar(
         image, orientation="horizontal", fraction=0.045
)

ax2 = cb.ax ##### 调用colorbar的ax属性
ax2.set_title('sea ice concentration (%)',fontsize=12) #####添加title

参考:python 调节colorbar的大小,以及对colorbar的细节调节-CSDN博客

你可能感兴趣的:(python,python,开发语言,jupyter)