0

I have some 2D matplotlib plots that have an area of no data below a certain value, as shown below (below the ~1.2 km line on the y-axis):

enter image description here

It seems that matplotlib automatically colors the area of no data with the color used for 0 on the colorbar scale. I feel like I've tried everything - masking the data, using set_under, etc. ....but none of it seems to be working. Here is where I'm at currently:

temperatures = ds_object['temperature'].values
time = ds_object['time'].values

inp_conc = ds_object['n_inp_stp'].values

vmin = 0.0
vmax = np.amax(inp_conc)

bad_color = 'lightgray'
cmap = cm.jet
cmap.set_under(color=bad_color)

mesh = display.fig.axes[0].pcolormesh(xrng,temperatures,inp_conc.transpose(),cmap=cmap,
    vmin=vmin, vmax=vmax)

fig = display.fig
ax = fig.axes[0]

ax.set_ylabel('Freezing Temperature (degC)')
ax.set_title(f"{datastream} {ds_object['n_inp_stp'].attrs['long_name']} on {date}")
myFmt = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(myFmt)

# Give the colorbar it's own axis so the 2D plots line up with 1D
box = ax.get_position()
pad, width = 0.01, 0.01
cax = fig.add_axes([box.xmax + pad, box.ymin, width, box.height])
cbar = plt.colorbar(mesh, cax=cax)
#get n_inp_stp min and max for colorbar
n_inp_stp = ds_object['n_inp_stp'].values
maximum = np.nanmax(n_inp_stp)
minimum = np.nanmin(n_inp_stp)
mesh.set_clim(minimum,maximum) # set colorbar range

cbar.ax.set_ylabel('Number of INP per L of air at STP (count/L)', rotation=270, fontsize=8, labelpad=8)
cbar.ax.tick_params(labelsize=6)
cbar.ax.set_yscale('log')

But for some reason, using the set_color hasn't been working. Does anyone have any idea as to what I'm doing wrong?

  • 1
    Your code contains mostly nonessentials and is not reproducible. *display*, **xrng(!)** and *ds_object* are not defined. I don't know what *inp_conc* could contain. – Michael Szczesny Oct 06 '21 at 19:59
  • What value does the 'area of no data' has? 0 oder `nan`? If it is zero you could use `xr.where()` to mask the data.. else you should make your example reproducible – mgraf Oct 07 '21 at 11:07
  • https://stackoverflow.com/questions/2578752/how-can-i-plot-nan-values-as-a-special-color-with-imshow-in-matplotlib the bit in the answer here about defining a colorbar and using `.set_bad()` may be helpful – ks905383 Oct 11 '21 at 17:13

0 Answers0