2015-06-05 36 views
3

我有一個散點圖,它繪製了來自兩個不同數據集的大量點。在某些區域,有大量的點,所以即使alpha值非常低(例如alpha = 0.1),也無法透視質量。但在這個alpha版本中,你幾乎看不到稀疏區域中的點。有沒有一種方法來限制疊加點的alpha值或以某種方式使背景在密集區域下可見,同時不會清除稀疏區域?散點圖對於點密集的區域,alpha仍然不透明

的代碼片段看起來是這樣的:

# Code to populate the datasets not included. 
fig, ax = plt.subplots() 
ax.scatter(x1, y1, s=12, color='red') 
ax.scatter(x2, y2, s=12, color='blue', alpha=0.1) 
# Plus code to do xlabels and such not included. 

產生這樣的:

enter image description here

正如你可以看到,這是很難看到底部紅腿的界限,仍然讓頂部的藍色拳腳出局。

有什麼辦法可以產生這種效果嗎?

在此先感謝。

編輯

一個很好的建議似乎是使用而不是分散的hexbin。這看起來很有希望,但顏色仍然不好混合。例如,

ax.hexbin(x1, y1, cmap='Reds', mincnt=1, vmax=100) 
ax.hexbin(x2, y2, cmap='Blues', mincnt=1, vmax=50, alpha=0.8, linewidths=0) 

產量:

enter image description here

這將是非常好的,使那些藍色和紅色合併。也許每個像素可能有來自一個數據集的R值,以及來自另一個數據集的B值或其他?但它似乎不是hexbin中的一個選項。

編輯

應用Thomasillo的回答後:

enter image description here

謝謝,我認爲它看起來比原來的更好。

+0

您可以考慮使用替代hexbin所示,[這個例子](http://matplotlib.org/examples/pylab_examples/hexbin_demo.html)。 – BrenBarn

+1

@BrenBarn hexbin在這裏有用嗎?這是一個兩組密度。我也不明白你們聯繫的例子是否應該解決這個問題。 –

+0

您可以使用兩個hexbins,就像您當前使用兩個散點一樣。如果你對兩個hexbins使用不同的顏色映射,並將兩個alpha設置爲一些非透明值,那麼你可以得到兩個在地方重疊的hexbins。我不認爲分散(即使是阿爾法)對於具有這種重疊的數據集來說是最好的選擇。 – BrenBarn

回答

1

1)爲了改善hexbin圖,你可以使用bins ='log'選項。這樣計算出對數六邊形裝倉的顏色,有效地使較低的數字比較高的數字更好。

2)自己計算每個數據集的密度。並且從這兩種密度產生一種顏色。讓一個密度影響紅色,另一個則是藍色通道。使用imshow繪製結果。

喜歡的東西

import matplotlib.pyplot as plt 
import numpy as np 
import itertools 

x1 = np.random.binomial(5100,0.5,51100) 
y1 = np.random.binomial(5000,0.7,51100) 
x2 = np.random.binomial(5000,0.5,51100) 
y2 = np.random.binomial(5000,0.7,51100) 


xmin,xmax,xnum = 2350,2700,50 
ymin,ymax,ynum = 3350,3700,50 
xx,yy=np.mgrid[xmin:xmax:xnum*1j,ymin:ymax:ynum*1j] 

def closest_idx(x,y): 
    idcs = np.argmin((xx-x)**2 + (yy-y)**2) 
    i_x,i_y = np.unravel_index(idcs, (xnum,ynum)) 
    return i_x,i_y 

def calc_count(xdat,ydat): 
    ct = np.zeros_like(xx) 
    for x,y in itertools.izip(xdat,ydat): 
     ix,iy = closest_idx(x,y) 
     ct [ix,iy] += 1 
    return ct 

ct1 = calc_count(x1,y1) 
ct2 = calc_count(x2,y2) 

def color_mix(c1 , c2): 
    cm=np.empty_like(c1) 
    for i in [0,1,2]: 
     cm[i] = (c1[i]+c2[i])/2. 
    return cm 

dens1 = ct1/np.max(ct1) 
dens2 = ct2/np.max(ct2) 

ct1_color = np.array([1+0*dens1 , 1-dens1 , 1-dens1 ]) 
ct2_color = np.array([1-dens2 , 1-dens2 , 1+0*dens2]) 

col = color_mix(ct1_color , ct2_color) 
col = np.transpose(col, axes=(2,1,0)) 


plt.imshow(col , interpolation='nearest' ,extent=(xmin,xmax,ymin,ymax),origin='lower') 
plt.show() 
+0

謝謝!我不得不在日誌空間中繪製我的比例,但結果很好。我會在我的問題中發佈它,以便您可以看到。 – ZSG

相關問題