You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Xarray绘图中Colorbar标签的字体大小?

修改Xarray绘图中Colorbar标签的字体大小

有两种简单的方法可以调整Colorbar标签的字体大小,均基于对接matplotlib的底层配置实现:

方法一:通过cbar_kwargs直接传递参数

在调用plot()方法时,直接在cbar_kwargs字典里添加fontsize参数,指定目标字体大小即可:

from cdasws import CdasWs
from cdasws.datarepresentation import DataRepresentation
import matplotlib.pyplot as plt

cdas = CdasWs()

# 获取数据部分代码不变
wind_dataset = cdas.get_datasets(observatoryGroup='Wind')
variables_wind = cdas.get_variables('WI_H1_WAV')
wind_data = cdas.get_data('WI_H1_WAV', ['E_VOLTAGE_RAD1','E_VOLTAGE_RAD2'],
                     '2020-07-11T02:15:00Z', '2020-07-11T02:50:00Z',
                     dataRepresentation = DataRepresentation.XARRAY)[1]

# 绘图并设置colorbar标签及字体大小
wind_data['E_VOLTAGE_RAD2'].transpose().plot(
    cbar_kwargs={
        'label': "This is the new label",
        'fontsize': 14  # 这里设置你需要的字体大小数值
    }
)
plt.yscale('log')
plt.show()

方法二:获取Colorbar对象后灵活调整

如果需要额外设置字体加粗、颜色等属性,可以先获取绘图返回的对象,再修改Colorbar标签的样式:

from cdasws import CdasWs
from cdasws.datarepresentation import DataRepresentation
import matplotlib.pyplot as plt

cdas = CdasWs()

# 获取数据部分代码不变
wind_dataset = cdas.get_datasets(observatoryGroup='Wind')
variables_wind = cdas.get_variables('WI_H1_WAV')
wind_data = cdas.get_data('WI_H1_WAV', ['E_VOLTAGE_RAD1','E_VOLTAGE_RAD2'],
                     '2020-07-11T02:15:00Z', '2020-07-11T02:50:00Z',
                     dataRepresentation = DataRepresentation.XARRAY)[1]

# 绘制图形并获取绘图对象
plot_obj = wind_data['E_VOLTAGE_RAD2'].transpose().plot(
    cbar_kwargs={'label': "This is the new label"}
)
# 修改colorbar标签的字体大小及其他属性
plot_obj.colorbar.set_label(
    label="This is the new label",
    fontsize=14,
    fontweight='bold',  # 可选:设置字体加粗
    color='darkblue'    # 可选:设置标签颜色
)
plt.yscale('log')
plt.show()

内容的提问来源于stack exchange,提问作者new-to-coding

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 17:42:25