【问题标题】:python colormap quantisation (matplotlib)python颜色图量化(matplotlib)
【发布时间】:2019-04-11 00:34:39
【问题描述】:

我在 Python 中有以下 colormap,它将每个值映射到一种颜色。但我的问题是:如何量化值以获得指定范围内的相同颜色? 例如:从 0 到 10(绿色), 从 10 到 50(黄色), 从 50 到 55(红色),...

import matplotlib as mpl
import matplotlib
from matplotlib import cm
.
.
.
norm = matplotlib.colors.Normalize(vmin = min,vmax = max, clip = True)
.
.
for i in range(numberMaterials):
    step = (max-min)/numberMaterials
    value = min + step*i
    mat = bpy.data.materials.new("mat" +str(i))
    color = cm.jet(norm(value),bytes=True)

【问题讨论】:

    标签: python-3.x matplotlib colormap


    【解决方案1】:

    看来您要的是BoundaryNorm

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.colors
    
    
    cmap = matplotlib.colors.ListedColormap(["limegreen", "gold", "crimson"])
    norm = matplotlib.colors.BoundaryNorm([0,10,50,55], 3)
    
    x = np.linspace(0,55)
    
    fig, (ax, ax2) = plt.subplots(ncols=2)
    sc = ax.scatter(x,x, c=x, cmap=cmap, norm=norm)
    fig.colorbar(sc, ax=ax, spacing="uniform")
    
    sc2 = ax2.scatter(x,x, c=x, cmap=cmap, norm=norm)
    fig.colorbar(sc2, ax=ax2, spacing="proportional")
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2017-06-19
      • 2014-10-19
      • 2020-10-07
      • 2014-05-05
      相关资源
      最近更新 更多