2017-12-20 11 views
0

파이썬에서 사용자 정의 주석으로 색상 그래디언트 사각형을 작성하려고합니다. matplotlib입니다. 아래는 값을 기준으로 색상을 지정하는 기능입니다. 참조 용 레이블을 만들려고합니다. 색상이 표시된 사각형이 있습니다. 다음과 같은 것을 찾고 있습니다 : https://stackoverflow.com/a/25679063/7733184 내 색상뿐 아니라 범위 내에서 내 기능대로 표시. 사용자 정의 그래디언트를 사용하여 색상이 채워진 직사각형 시퀀스

def returncolor(value,colors): 
    if value < 0.55: 
     return '#B03A2E' #darkest red 
    if value < 0.60: 
     return '#EC7063' # light red 
    if value < 0.65: 
     return '#FCF3CF' # lighest yellow 
    if value < 0.70: 
     return '#F1C40F' # yellow 
    if value < 0.75: 
     return '#F39C12' # Orange 
    if value < 0.80: 
     return '#82E0AA'#light green 
    if value < 0.85: 
     return '#28B463'#dark green 
    if value < 0.90: 
     return '#7FB3D5'#light blue 
    if value < 0.95: 
     return '#2980B9'#dark blue 
    if value < 1: 
     return '#5B2C6F'#dark blue 

내가 뭘하려고 오전의 모형

은 다음과 같습니다 :

enter image description here

+0

출력물의 모양을 나타 내기 위해 목업자 이미지를 질문에 추가 할 수 있습니다. – pts

+0

@DizietAsahi 정확히 동일하지는 않지만 그라디언트가 아닙니다. 이들은 사용자 정의 색상입니다. –

답변

1

the answer to this question 당, 당신은 ListedColormap 객체를 사용하여 색상 맵 사용자 정의를 정의 할 수 있습니다.

enter image description here

colors = ['#B03A2E','#EC7063','#FCF3CF','#F1C40F','#F39C12','#82E0AA','#28B463','#7FB3D5','#2980B9','#5B2C6F'] 
bounds = [0.5,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1] 
cmap = matplotlib.colors.ListedColormap(colors) 
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N) 

zvals = np.random.rand(100, 100) * 10 

# tell imshow about color map so that only set colors are used 
img = plt.imshow(zvals, cmap=cmap, norm=norm) 

# make a color bar 
plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=bounds) 

plt.show() 

는하기 matplotlib의 설명서에서도 the example 참조하십시오.

+0

저는 실제로 오른쪽의 눈금을 찾고있었습니다. 아니 zvals 또는 임의의 줄거리. –