0

I have gridded netcdf file (time, depth, lon, lat) of oxygen. I need a profile plot as shown below. What I'm trying to do is average a range of latitudes and longitudes, then lot time vs. oxygen depth (at a given range) as a fill plot. Using the commands below, I plotted a fill plot.

fig, ax = plt.subplots(figsize=(12, 8))

time_sub    = var_sel.sel(time=slice('2000-01-01 00:00', '2018-12-31 12:00'))
oxy_boxmean_3 = time_sub.sel(depth=slice(0,800),longitude=slice(75.4,76.5),
                           latitude=slice(8,9)).mean(dim=['longitude','latitude'])

oxy_boxmean_3.T.plot()
plt.gca().invert_yaxis()

The resultant figure is attached below.enter image description here

But now I want to customize the colorbar position and distance from plot (there is some white space between the figure and colorbar, I need to reduce it). I tried imshow() and plt.colorbar(), but it is not working (probably due to the way I subsetted my data using the above code).

Can anyone help me to do that?. I am beginner in python. Any help is much appreciated.

  • 1
    Does this answer your question? [Positioning the colorbar](https://stackoverflow.com/questions/13310594/positioning-the-colorbar) – warped Feb 27 '22 at 14:33
  • No. The way I subsetted my data using the above code is not allowing me to use imshow() and plt.colorbar(). – linux_lover Feb 27 '22 at 14:46
  • 2
    And what is the underlying library? Are we supposed to guess what `XYZ.sel()` returns and is then used for the plot of the same library? It might be Xarray, which accepts [cbar kwargs](https://stackoverflow.com/a/71236439/8881141). But please provide [a minimal, complete, and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Mr. T Feb 27 '22 at 16:31
  • Instead of using xarray's plot command (I assume), just use Matplotlib's pcolormesh, and then place the colorbar using the shrink, aspect, and pad kwargs. – Jody Klymak Feb 27 '22 at 18:52

0 Answers0