2014-05-19 57 views
1

我寫了一個生成不同尺寸的矩形的Python代碼。但是,我無法使edgecolor與facecolor相匹配。以下是我的代碼的相關部分:Matplotlib的PatchCollections與set_array:如何匹配臉部顏色和邊緣顏色?

# Mapping row information to geometry and color of plot points 
x = np.array(info[0])-time 
y = np.array(info[1]) 
x_width = np.array(info[2]) 
y_width = np.array(info[3]) 
colors = np.array(info[4]) 
colors = np.log10(colors) 

patches = [] 

# Save rectangle object-datapoints to patches 
(time_min,time_max) = (x-x_width/2., x+x_width/2.) 
(freq_min, freq_max) = (y-y_width/2., y+y_width/2.) 

for i in range(len(x)): 
    f_min_plot = max(freq_min[i],f_min) 
    patches.append(Rectangle((time_min[i], f_min_plot), x_width[i], freq_max[i]-f_min_plot)) 

fig = pl.figure() 
pl.rc('text', usetex=True) 
ax = fig.add_subplot(111) 
p = PatchCollection(patches, match_original=False) 
p.set_array(colors) 
ax.add_collection(p) 
cbar = pl.colorbar(p) 
#cbar.set_label('$\log_{10}$ SNR', rotation = 270) 
pl.xlabel("Time-%.1f" %(time)) 
pl.ylabel("Frequency") 
pl.semilogy() 
pl.title("%s1 omicron triggers"%(detector)) 
pl.xlim(-time_interval,time_interval) 
pl.ylim(20,2000) 
pl.savefig("%s/%s1-omicron-trigs-%.1f-%d.png" %(file_path, detector, time, time_interval)) 

此代碼生成具有黑色邊框的默認矩形facecolors。我需要邊框顏色來匹配facecolor。請注意,我試圖刪除edgecolor =「none」的邊框。不幸的是,這使得看到一些矩形(寬度較小的矩形)的顏色相當困難。我也嘗試在Rectangle()語句中添加color = matplotlib.cm.jet(color_norm [i]#color_norm是「標準化」顏色數組:color/max(color))(當然,在PatchCollections中match_original = True ()語句)。但那也行不通。在我看來,set_array()最終決定了顏色。我無法找到關於set_array究竟做什麼以及如何使用它的詳細文檔。

有沒有什麼辦法讓這個代碼繪製出具有相同的人臉顏色和邊緣顏色的矩形?我衷心感謝在這個問題上的任何幫助。

回答

1

您可以使用這樣的'face'鍵字使用set_edgecolor命令:

p.set_edgecolor('face') 

這是否把戲對我來說,並設置邊的顏色相同的值作爲外觀顏色。