Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

python colormap quantisation (matplotlib)

I have the following colormap in Python which maps each value to a color. But my question: How can I quantize the values for getting a same color for a specified range? For example: from 0 until 10 (Green) , from 10 until 50 (yellow), from 50 until 55 (red) , ...

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you are asking for a 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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...