Matplotlib Colorbar - 非线性
Matplotlib Colorbar - Non-Linear
我创建了一个发散色条,它的中点在数据的中值处标准化。
我想扩展中点颜色 ('white') 并将其应用于从中点开始的范围 (+- 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)
您可以通过从 .5 - .15
到 .5 + .15
插入一个白色部分(即所有三种颜色的 1.
)来创建具有 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')
我创建了一个发散色条,它的中点在数据的中值处标准化。 我想扩展中点颜色 ('white') 并将其应用于从中点开始的范围 (+- 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)
您可以通过从 .5 - .15
到 .5 + .15
插入一个白色部分(即所有三种颜色的 1.
)来创建具有 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')