2017-06-12 83 views
3

Example Plot蟒蛇matplotlib傳說不透明度

x = [1, 2, 3, 4, 5] 
y = [2, 3, 5, 6, 4] 
c = [(1, 0, 0, 1),(1, 0, 0, .8),(0, 0, 0, .5),(0, 0, 0, .8),(1, 0, 0, .3)] 

plt.scatter(x,y,c=c,s=55) 
plt.legend(handles=[mpatches.Patch(color='red',label='Type1'), 
        mpatches.Patch(color='black',label='Type2')]) 
plt.show() 

我繪製一個數據集有點類似於上述之一。在我的數據集中,顏色表示數據點的分類,不透明度表示其錯誤的大小(數據集相當密集並且使錯誤條不可行)。

我想知道是否可以創建某種不透明度的圖例,也許是一系列黑點,從0到1的不透明度各不相同,每個黑點都標有相關的錯誤。

謝謝!

回答

3

你可以用空散點圖把手,其中散點阿爾法值變化再添傳奇。

例如使用具有6個不同的混濁範圍從0到1,可以這樣做:

import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 

x = [1, 2, 3, 4, 5] 
y = [2, 3, 5, 6, 4] 
c = [(1, 0, 0, 1),(1, 0, 0, .8),(0, 0, 0, .5),(0, 0, 0, .8),(1, 0, 0, .3)] 

plt.scatter(x,y,c=c,s=55) 
leg1 = plt.legend(handles=[mpatches.Patch(color='red',label='Type1'), 
        mpatches.Patch(color='black',label='Type2')], loc="upper left") 
plt.gca().add_artist(leg1) 

error = [0,.2,.4,.6,.8,1] 
h = [plt.scatter([],[],s=55, c=(0,0,0,i)) for i in error] 
plt.legend(h, error, loc="upper right") 

plt.show() 

enter image description here

2

如果使用亮度而不是不透明度來表示錯誤是一個選項,則可以使用預定義的顏色圖來顯示如下所示的顏色條。否則,我認爲你可以嘗試定義你自己的色彩地圖。

from matplotlib import pyplot as plt 
from matplotlib import patches as mpatches 

t1_x = [1, 2, 5] 
t1_y = [2, 3, 4] 
t1_err = [1, .8, .3] 
t2_x = [3, 4] 
t2_y = [5, 6] 
t2_err = [.5, .8] 

plt.figure(figsize=[8, 4]) 
t1_sc = plt.scatter(t1_x, t1_y, s=55, vmin=0, vmax=1, 
        c=t1_err, cmap=plt.cm.get_cmap('Reds')) 
t2_sc = plt.scatter(t2_x, t2_y, s=55, vmin=0, vmax=1, 
        c=t2_err, cmap=plt.cm.get_cmap('Greys')) 
plt.colorbar(t1_sc) 
plt.colorbar(t2_sc) 
plt.legend(handles=[mpatches.Patch(color='red',label='Type1'), 
        mpatches.Patch(color='black',label='Type2')]) 
plt.show() 

enter image description here