Python Matplotlib颜色条-非线性

Python Matplotlib颜色条-非线性,python,matplotlib,plot,colorbar,Python,Matplotlib,Plot,Colorbar,我创建了一个发散的色条,它的中点在数据的中值处标准化。 我想扩展中点颜色(“白色”)并将其应用于距离中点(+-15%)的范围,然后让发散的颜色条从该点正常继续 我当前的颜色栏是使用以下代码创建的: #Initial ZValues contour plot Colorbar_min = np.around(ZValues.min()*0.9,0) Colorbar_max = np.around(ZValues.max()*1.1,0) Colorbar_mid = np.median(ZVal

我创建了一个发散的色条,它的中点在数据的中值处标准化。 我想扩展中点颜色(“白色”)并将其应用于距离中点(+-15%)的范围,然后让发散的颜色条从该点正常继续

我当前的颜色栏是使用以下代码创建的:

#Initial ZValues contour plot
Colorbar_min = np.around(ZValues.min()*0.9,0)
Colorbar_max = np.around(ZValues.max()*1.1,0)
Colorbar_mid = np.median(ZValues)


#Colormap
cmap = plt.cm.seismic  # define the colormap
cmaplist = [cmap(i) for i in range(cmap.N)]  # extract all colors from the .seismic map

# create the new colourmap
cmap = mpl.colors.LinearSegmentedColormap.from_list('Custom cmap', cmaplist, cmap.N)

# define the bins and normalize
bounds = np.linspace(Colorbar_min, Colorbar_max, 30)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

Chosen_CS = ax.tricontourf(Chosen_tri_refi, Chosen_Z_refi, cmap=cmap, levels=bounds,
                           norm=MidpointNormalize(midpoint=Colorbar_mid, vmin=Colorbar_min, vmax=Colorbar_max))

#Create a second axis for the colorbar
ax2 = fig.add_axes([0.87, 0.12, 0.04, 0.75]) #The numbers in the square brackets of add_axes refer to [left, bottom, width, height], where the coordinates are just fractions that go from 0 to 1 of the plotting area.
cb = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=MidpointNormalize(midpoint=Colorbar_mid, vmin=Colorbar_min, vmax=Colorbar_max),spacing='uniform', ticks=bounds, boundaries=bounds, format='%1i')
cb.set_label('ZValues', fontsize=7, weight="bold", rotation=270, labelpad=14)

您可以通过插入一个白色部分(即所有三种颜色的
1.
)从
.5-.15
.5+.15
来创建具有50±15%白色中间颜色的自定义颜色贴图,如下所示:

import matplotlib.pyplot as plt
import matplotlib as mpl

seismic_cdict = plt.cm.seismic._segmentdata
cdict = dict()
for c in seismic_cdict:
    cdict[c] = [t for t in seismic_cdict[c] if t[0] < .35] + \
                   [(.35,1.,1.), (.65,1.,1.)] + \
                   [t for t in seismic_cdict[c] if t[0] > .65]
custom_cmap = mpl.colors.LinearSegmentedColormap('Custom cmap', cdict)

fig, ax = plt.subplots(figsize=(8, 1))
mpl.colorbar.ColorbarBase(ax, cmap=custom_cmap, orientation='horizontal')
导入matplotlib.pyplot作为plt
将matplotlib导入为mpl
地震数据=plt.cm.地震数据
cdict=dict()
对于地震仪中的c:
cdict[c]=[t表示地震中的t_cdict[c]如果t[0]<.35]+\
[(.35,1.,1.), (.65,1.,1.)] + \
[t代表地震中的t[c]如果t[0]>.65]
自定义cmap=mpl.colors.LinearSegmentedColormap('custom cmap',cdict)
图,ax=plt.子批次(图尺寸=(8,1))
mpl.colorbar.colorbase(ax,cmap=custom\u cmap,orientation='horizontal')