【python海洋专题十五】给colorbar加单位
上期内容
读取多个温度nc文件,画年平均和季节平均的温度分布。但是图片的colorbar没有单位,°C。
本期内容
给colorbar加单位,两种方法:
第一种共用一单位°C;
第二种谁用谁的单位。
如下:
共用一个:
谁用谁的:
方法如下:
共用一个:
cb.set_label('温度(°C)', fontsize=4, color='k') # 设置color-bar的标签字体及其大小
谁用谁的:
cb.set_ticks([20, 22, 24, 26, 28, 30])
cb.set_ticklabels(['20°C', '22°C', '24°C', '26°C', '28°C', '30°C'])
全文代码
# -*- coding: utf-8 -*-
# %%
# Importing related function packages
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import numpy as np
import matplotlib.ticker as ticker
from cartopy import mpl
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from matplotlib.font_manager import FontProperties
from netCDF4 import Dataset
from pylab import *
import seaborn as sns
from matplotlib import cm
from pathlib import Path
import xarray as xr
import palettable
from palettable.cmocean.diverging import Delta_4
from palettable.colorbrewer.sequential import GnBu_9
from palettable.colorbrewer.sequential import Blues_9
from palettable.scientific.diverging import Roma_20
from palettable.cmocean.diverging import Delta_20
from palettable.scientific.diverging import Roma_20
from palettable.cmocean.diverging import Balance_20
from matplotlib.colors import ListedColormap
# ----define reverse_colourmap----
def reverse_colourmap(cmap, name='my_cmap_r'):
reverse = []
k = []
for key in cmap._segmentdata: