Home > front end >  How to change colorbar label in matplotlib quadmesh
How to change colorbar label in matplotlib quadmesh

Time:02-24

I have an xarray DataArray with unevenly spaced coordinates that looks like this:

print(da)

<xarray.DataArray 'Sa' (y: 679, x: 87)>
array([[1.      , 0.677667, 0.658552, ..., 0.583562, 0.443248, 0.366353],
       [0.799933, 0.929478, 1.      , ..., 0.386675, 0.316399, 0.284364],
       [0.952748, 0.844993, 0.793359, ..., 0.724242, 0.554958, 0.295785],
       ...,
       [0.983343, 0.98983 , 1.      , ..., 0.601416, 0.592084, 0.586747],
       [0.997548, 1.      , 0.99682 , ..., 0.596712, 0.591876, 0.58533 ],
       [0.995427, 1.      , 0.990861, ..., 0.581317, 0.588433, 0.585797]])
Coordinates:
  * y        (y) float64 -0.1274 -0.1224 -0.1174 -0.1124 ... 3.253 3.258 3.263
  * x        (x) float64 4.002 3.971 3.961 3.95 3.94 ... 3.407 3.401 3.396 3.39

I can plot it with the DataArray.plot method and get a matplotlib QuadMesh object:

da.plot()

enter image description here

I cannot figure how to change the colorbar label, which is automatically set to the DataAray name "Sa".

CodePudding user response:

As a plot of 2D data initiates in xarray the plotting using pcolormesh, you can set the colorbar label providing a cbar_kwargs dictionary that is handed down to matplotlib:

da.plot(cbar_kwargs={'label': "This is the new label"})

For a full list of possible arguments, see the keywords in the matplotlib documentation.

  • Related