Вы можете использовать комбинацию LinearSegmentedColormap
для создания карты цветов и DiverginNorm
для определения конечных и центральных точек.
Демонстрация (код не оптимизирован, но он показывает общую идею):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib import colors
maxV = 100
minV = -100
centerV = -50
N=10
data = np.random.uniform(low=minV, high=maxV, size=(N,N))
cmap = colors.LinearSegmentedColormap.from_list('', ['blue','lime','red'])
norm = colors.DivergingNorm(vmin=minV, vcenter=centerV, vmax=maxV)
fig, (ax, axSlide1, axSlide2, axSlide3) = plt.subplots(4,1, gridspec_kw=dict(height_ratios=[100,5,5,5]))
im = ax.imshow(data, cmap=cmap, norm=norm)
cbar = fig.colorbar(im, ax=ax)
axcolor = 'lightgoldenrodyellow'
for sax in [axSlide1, axSlide2, axSlide3]:
sax.set_facecolor(axcolor)
smin = Slider(axSlide1, 'min', minV, maxV, valinit=minV)
scenter = Slider(axSlide2, 'center', minV, maxV, valinit=centerV)
smax = Slider(axSlide3, 'max', minV, maxV, valinit=maxV)
def update(val):
global cbar
minV = smin.val
maxV = smax.val
centerV = scenter.val
if minV>maxV:
minV=maxV
smin.set_val(minV)
if maxV<minV:
maxV=minV
smax.set_val(maxV)
if centerV<minV:
centerV = minV
scenter.set_val(centerV)
if centerV>maxV:
centerV = maxV
scenter.set_val(centerV)
#redraw with new normalization
norm = colors.DivergingNorm(vmin=minV, vcenter=centerV, vmax=maxV)
ax.cla()
cbar.ax.cla()
im = ax.imshow(data, cmap=cmap, norm=norm)
cbar = fig.colorbar(im, cax=cbar.ax)
fig.canvas.draw_idle()
smin.on_changed(update)
smax.on_changed(update)
scenter.on_changed(update)
plt.show()
![enter image description here](https://i.stack.imgur.com/e6D5y.gif)