2015-04-30 104 views
0
import brewer2mpl 
import numpy as np 

a = np.random.rand(3) # a[0] represents value of rect1, a[1] of rect[2].... 

def get_colors():  
    """ 
    Get colorbrewer colors, which are nicer 
    """    
    bmap = brewer2mpl.get_map('Accent','qualitative',6) 
    return bmap.mpl_colors 

rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color='yellow')) 
ax.add_patch(rect1) 
rect2 = matplotlib.patches.Rectangle((3,3), 1, 1, color='green')) 
ax.add_patch(rect2) 
rect3 = matplotlib.patches.Rectangle((5,5), 1, 1, color='red')) 
ax.add_patch(rect3) 

我希望矩形的顏色根據向量'a'的值而變化。而不是純黃色/綠色/紅色,選擇一個範圍內的顏色,最好是brewer2mpl顏色Python繪製矩形和範圍內的顏色

回答

1

從我能讀的,mpl_color是顏色列表。顏色是3元組,範圍在(0,1)代表rgb數量。你可以將顏色設置爲這樣一個三元組。

import pylab as py 

py.plot([0,1],[2,3], color = (1,0,0.)) 
py.savefig('tmp.png') 

enter image description here

因此,所有你需要做的就是採取矢量a項(我稱之爲avalue下方,0<avalue<1)並將其映射到一個合適的整數。例如,

colorlist = get_colors() 
colorlength = len(colorlist) 
py.plot([0,1],[2,3], color = colorlist[int(avalue*colorlength)]) 
1

您不一定需要創建一個np數組來保存隨機索引到您要使用的顏色。

爲了一種顏色分配給每個矩形,您可以通過顏色容易使用週期itertools:

import brewer2mpl 
import numpy as np 
import itertools 

color_map = brewer2mpl.get_map('Accent', 'qualitative', 6) 
colors = itertools.cycle(color_map.mpl_colors) 

rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=next(colors)) 

這將指定顏色依照原來的序列。如果你想隨機的顏色,你可以這樣做很容易:

import random 
import itertools 

color_map = brewer2mpl.get_map('Accent', 'qualitative', 6) 
colors = [c for c in color_map.mpl_colors] 
random.shuffle(colors) 
rnd_colors = itertools.cycle(colors) 

rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=next(rnd_colors)) 

如果你確定你有足夠的顏色供您rects,你並不真的需要週期,可以只是彈出一些隨機:

color_map = brewer2mpl.get_map('Accent', 'qualitative', 6) 
colors = [c for c in color_map.mpl_colors] 
random.shuffle(colors) 

rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=colors.pop()) 
+0

random.choice(序列)是另一個偉大的方式來獲得一個隨機選擇,但你不想在這裏使用它,因爲你可能得到重複的顏色! – jminuscula

+0

謝謝,但我想根據範圍分配顏色。所以不是隨機分配,較低的值應該從較高的值獲得不同的顏色... – user308827

+0

您的意思是不同的值應該分組到相同的顏色,所以值0 - 25將是紅色,26 - 50藍色等。檢查Joel的答案,你可以直接訪問並分配mpl_colors的顏色元組,你只需要找出與每個範圍對應的索引。 – jminuscula

1

不確定colorbrewer或a的使用是否更重要,它們看起來不太適合同時使用。有幾種方法:

import brewer2mpl 
import numpy as np 
import matplotlib 


bigness = 8 
a = np.random.rand(bigness) # a[0] represents value of rect1, a[1] of rect[2].... 

def get_colors(numpatches): 
    """ 
    Get colorbrewer colors, which are nicer 
    """ 
    bmap = brewer2mpl.get_map('Accent','qualitative', numpatches) 
    return bmap.mpl_colors 


fig, axs = matplotlib.pyplot.subplots(ncols=3, nrows=1) 
colors = get_colors(bigness) 

for i in (0,2,3,5,bigness-1): 
    rect = matplotlib.patches.Rectangle((i,i), 1, 1, color=colors[i]) 
    axs[0].add_patch(rect) 
axs[0].set_title('Color evenly spaced\nfrom colorbrewer') 

patches = [] 
for i in (0,2,3,5,bigness-1): 
    rect = matplotlib.patches.Rectangle((i,i), 1, 1) 
    patches.append(rect) 
collection = matplotlib.collections.PatchCollection(patches, 
                cmap=matplotlib.cm.hot) 
collection.set_array(a) 
collection.set_edgecolor('none') 
axs[1].add_collection(collection) # and why not? 
axs[1].set_title('Color from\nrandom array a') 


patches = [] 
for i in a: 
    loc = i*bigness 
    rect = matplotlib.patches.Rectangle((loc,loc), 1, 1) 
    patches.append(rect) 
collection = matplotlib.collections.PatchCollection(patches, 
                cmap=matplotlib.cm.hot) 
collection.set_array(a) 
collection.set_edgecolor('none') 
axs[2].add_collection(collection) # and why not? 


axs[2].set_title('Color matches location\nboth from a') 

for ax in axs: 
    ax.set_xlim((0,bigness)) 
    ax.set_ylim((0,bigness)) 

from os.path import realpath, basename 
s = basename(realpath(__file__)) 
fig = matplotlib.pyplot.gcf() 
fig.savefig(s.split('.')[0]) 

matplotlib.pyplot.show() 

enter image description here

+0

第三個選項使得「基於範圍分配顏色,所以不用隨機分配,較低的值應該從較高的值獲得不同的顏色」。 – cphlewis