'How to edit the colorbar in Matplotlib so that it splits on a specific color plus a colormap?
I have x, y, z data plotted with contourf. My z values varies in this interval (0, 3.6). I want that all the z values <0.35 are colored in blue (for example, blue(Safe 16 SVG Hex3) #0000FF), and the rest of the z values use a colormap that includes no blue at all, for example, tab20b.
This is the code I have so far. No errors, but I don't get the blue color.
#Grid & Interpolation
X = np.linspace(min(x), max(x),num=444)
Y = np.linspace(min(y), max(y),num=14)
X, Y = np.meshgrid(X, Y) # 2D grid for interpolation
interp = LinearNDInterpolator(list(zip(x, y)), z)
Z = interp(X, Y)
cmap = mpl.cm.get_cmap("tab20b").copy()
cmap.set_under('blue')
bounds = [0,0.35,0.6, 0.8, 1, 1.2, 1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
#Plot
fig,ax1 = plt.subplots(figsize=(20,20))
imcontour=ax1.contourf(X, Y, Z,cmap=cmap,norm=norm,levels=19)
ax1.set_title(survey_name,{'fontsize': 24},weight='bold')
cb1=plt.colorbar(imcontour,orientation="horizontal",pad=0.05,shrink=0.3).set_label(label='Resistivity Ohm-m',size=12)
ax1.set_xlabel("X [m]",{'fontsize': 12})
ax1.set_ylabel("Depth [m]",{'fontsize': 12})
`
I appreciate your help. Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
