I have a dataset (agree_all_land) that only contains whole numbers (i.e. 0, 1, 2, 3, 4, 5). When I plot it, I get the following:
However, it is unclear which color represents 0 versus 1 for example. I would like each value to have it's own color. That is, in the colorbar there should be six colors (one each for 0, 1, 2, 3, 4, 5) and the tick for each should be centered to that color. How do I do this? Here is my code for the plot:
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=[10,8])
agree_all_land.topo.plot(cmap='viridis', levels=np.linspace(0,5,6))
axes.set_xlabel('longitude (degrees east)')
axes.set_ylabel('latitude (degrees north)')
You can try this:
import xarray as xr
import numpy as np
coords = dict(lon=np.arange(360), lat=np.arange(90))
nlon, nlat = len(coords["lon"]), len(coords["lat"])
low, high = 0, 8
ticks = np.arange(low, high + 1)
data = np.random.randint(low=0, high=8, size=(nlon, nlat))
da = xr.DataArray(data, coords=coords)
plot = da.plot.pcolormesh(
levels=ticks - 0.5,
cbar_kwargs=dict(ticks=ticks),
)