2015-04-30 97 views
0

我需要爲連接由第三個變量着色的散點圖的線着色(第三個變量對於所有散點是相同的;我將有多個散點圖最後的第三個變量)。我需要線條的顏色來匹配散點,並且需要將顏色條按比例縮放。我無法提取RGBA對數歸一化值,用於對散點進行着色,以便按該值對行進行着色。下面的示例:matplotlib:通過對數顏色條值用於顏色散點圖的彩色2D線

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

color = plt.get_cmap('Blues') 

#Fake data 
a = np.arange(0,10,1) 
b = np.arange(10,20,1) 
d = [100]*10 

maxval=1000.0 
minval=10.0 

#Normalize array to limits of colorbar 
l=d[1] 
normalized= (l/(maxval-minval)) 

#Check if Nan (I have some NaN's). 
#Returns the colormap value 
check = np.isnan(np.sum(normalized)) 
cmapvalue=[] 
if check==True: 
    cmapvalue=g 
else: 
    cmapvalue=color(normalized) 

#Plot scatter and line, line needs to be colored by RGBA value used to color scatter points 
plt.scatter(a, b, c=d, cmap=color, norm=mpl.colors.LogNorm(vmax=maxval,  vmin=minval), zorder=2, s=50) 
plt.plot(a,b, c=cmapvalue, zorder=1, lw=4) 

plt.colorbar() 
plt.show() 

任何幫助,將不勝感激

+0

能否請你也provid'D'?或提供一個合適的替代品? – hitzg

+0

更新爲d ...道歉 – emunson5

回答

0

所以問題是,你(手動)線性擴展你的價值觀。但在分散呼叫中,您傳遞了一個對數縮放類。而不是僅僅通過它,你可以直接用它來標準化值您的來電情節:

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

#Fake data 
a = np.arange(0,10,1) 
b = np.arange(10,20,1) 
d = [50]*10 

# define vmin and vmax 
maxval=1000.0 
minval=10.0 

# build up colormap and normalizer 
colormap = plt.get_cmap('Blues') 
norm = mpl.colors.LogNorm(vmax=maxval, vmin=minval) 

# helper function to plot the line and the scatter data 
def plot_my_scatterdata(x, y, d): 
    if np.any(np.isnan(d)): 
     color = 'g' 
    else: 
     # use the colormap and the normalization instance! 
     color = colormap(norm(d[0])) 

    plt.scatter(a, b, c=d, cmap=colormap, norm=norm, zorder=2, s=50) 
    plt.plot(a,b, '-', color=color, zorder=1, lw=4) 

plot_my_scatterdata(a, b, d) 
d = [100]*10 
b += 1 
plot_my_scatterdata(a, b, d) 
d = [500]*10 
b += 1 
plot_my_scatterdata(a, b, d) 
d[0] = np.nan 
b += 1 
plot_my_scatterdata(a, b, d) 

plt.colorbar() 
plt.show() 

結果: enter image description here