How to change font size of xarray facetgrid colorbar labels?
I'm using xarray's facetgrid to plot data from a dataArray. I want to plot the 12 months of data in grid form and insert the colorbar on the side. xarray already does all that, but I'm not sure how I can increase the font size of the colorbar label and the colorbar ticklabels. Here's an example to be reproduced:
import pandas as pd
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
np.random.seed(0)
temperature = 15 + 8 * np.random.randn(49, 49, 12)
lon = np.linspace(-52, -40, 49)
lat = np.linspace(-15, -27, 49)
month = np.arange(1, 13, 1)
da = xr.DataArray(
data=temperature,
dims=["lon", "lat", "month"],
coords=dict(
lon=("lon", lon),
lat=("lat", lat),
month=month,
),
attrs=dict(
description="Ambient temperature.",
units="degC",
),
)
fg = da.plot(x='lon',
y='lat',
col='month',
col_wrap=4,
cmap='GnBu',
figsize=(27, 15),
vmin=0,
vmax=50,
transform=ccrs.PlateCarree(),
subplot_kws={
"projection": ccrs.PlateCarree()
},
cbar_kwargs={
"orientation": "vertical",
#"shrink": 1.,
#"aspect": 40,
"label": "Temperature (°C)",
})
titles = ['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
for ax, title in zip(fg.axes.flatten(), titles):
ax.set_title(title, fontsize=22)
Which gives me this figure:
Thank you very much in advance.
Robson
Solution 1:
One way is to remove the colorbar label definition from cbar_kwargs
and define it in the end together with the size of the colorbar ticks. This gives more fine grained control:
fg = da.plot(x='lon',
y='lat',
col='month',
col_wrap=4,
cmap='GnBu',
figsize=(27, 15),
vmin=0,
vmax=50,
transform=ccrs.PlateCarree(),
subplot_kws={
"projection": ccrs.PlateCarree()
},
cbar_kwargs={
"orientation": "vertical",
#"shrink": 1.,
#"aspect": 40
})
titles = ['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
for ax, title in zip(fg.axes.flatten(), titles):
ax.set_title(title, fontsize=22)
fg.cbar.ax.tick_params(labelsize=30)
fg.cbar.set_label(label='Temperature (°C)', size=30, weight='bold')