Matplotlib:如何使用用户输入更改颜色栏渐变

2024-09-25 02:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用特定的值范围自定义颜色栏(colormap)。颜色范围应随给定参数(Tup、Tmd、Tbt)而变化,其中

  • Tup:用户选择的上限值
  • Tmid:用户选择的中点
  • Tbt:用户选择的底部点

中间颜色(石灰色)的范围应通过用户选择的Tup和Tbt,Tmd作为中间点

enter image description here

我试图使用下面的代码片段生成自定义颜色映射,但无法使用用户提供的值控制其范围

cmap = LinearSegmentedColormap.from_list("", ["blue","gray","lime","gray","red"]) cax = ax.pcolor(data,cmap=cmap,edgecolors='k',vmin=0,vmax=100)

如何根据用户输入控制颜色映射值


Tags: 代码用户from参数颜色tbtcmap灰色
1条回答
网友
1楼 · 发布于 2024-09-25 02:27:22

您可以使用^{}的组合来创建颜色贴图,使用^{}来定义端点和中心点

演示(代码未优化,但它显示了总体思路):

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

相关问题 更多 >