2016-10-06 2 views
0

이산 색상 목록에서 선형 색상 맵을 만들고 기본 RGB 값을 추출하고 싶습니다. 필자는 matplotlib 문서의 예제 스크립트를 사용하여 첫 번째 단계를 수행했습니다.LinearSegmentedColormap을 사용하여 만든 선형 컬러 맵에서 RGB 값을 추출하십시오.

from matplotlib import cm 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.colors import LinearSegmentedColormap 

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] 
colormap = LinearSegmentedColormap.from_list('colormapX', colors, N=100) 

x = np.arange(0, np.pi, 0.1) 
y = np.arange(0, 2*np.pi, 0.1) 
X, Y = np.meshgrid(x, y) 
Z = np.cos(X) * np.sin(Y) * 10 

fig, ax = plt.subplots() 
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=colormap) 
fig.colorbar(im, ax=ax) 
plt.show() 

이 컬러 맵은 원래의 세 가지 색상 보간로부터 유도 된 컬러 (100)에 기초한다. 어떻게이 100 가지 색상의 RGB 값을 가진 ndarray를 추출합니까?

+0

어쩌면 내 대답은 [여기] (http://stackoverflow.com/questions/39885178/how-can-i-see-the-rgb-channels-of-a-given-image-with-python) 도울 수있다. – sascha

답변

1

어딘가에서 직접 사용할 수 있는지, 또는 colormap에 주어진 값을 평가하도록 요청할 수 있는지 (주어진 숫자에 해당하는 색상을 반환) 모르겠지만이 간단한 경우에는 목록을 직접 만들 수 있습니다 :

def color_interpolation(c1, c2, fraction=0.5): 
    return ((1.-fraction)*c1[0] + fraction*c2[0], 
      (1.-fraction)*c1[1] + fraction*c2[1], 
      (1.-fraction)*c1[2] + fraction*c2[2],) 

def make_color_interpolation_list(colors, N): 
    n_colors = len(colors) 
    n_steps_between_colors = N/(n_colors-1) 
    fraction_step = 1./n_steps_between_colors 
    color_array = np.zeros((N,3)) 
    color_index = 0 
    while color_index < n_colors-1: 
     fraction_index = 0 
     while fraction_index < n_steps_between_colors: 
      index = color_index*n_steps_between_colors+fraction_index 
      color_array[index]= color_interpolation(c1=colors[color_index], 
            c2=colors[color_index+1], 
            fraction=fraction_index*fraction_step) 
      fraction_index += 1 
     color_index += 1 
    if index != len(color_array)-1: 
     color_array[-1] = colors[-1] 
    return color_array 
+1

일부 조합 된 매개 변수를 제외하고는 색상 표의 끝 부분에 원치 않는 검은 색 색조가 추가되는 것을 제외하고는 거의 완벽합니다. [red, green, blue]와 [N = 17]을 시도해보십시오. – themachinist

+0

모든 숫자가 다른 사람들로 깔끔하게 나뉘는 것은 결코 기억하지 못하는 것 같습니다. 가장 깊은'while' 안에'index = color_index * n_steps_between_colors + fraction_index'를 만들고 끝에는'index'가'len (color_array) -1'과 다른지 아닌지 테스트합니다. 그것이 다르다면'color_array [-1] = colors [-1]'을 만들어라. [위의 코드에서 편집] 결국 jerry 조작 된 솔루션입니다. – berna1111

관련 문제