2017-08-10 70 views
1

需要在一個繪圖中繪製8種不同函數的CDF。它只給出7種不同顏色和8種顏色的問題再次給出了第一種藍色。如何製作8種不同的顏色?CDF,matplotlib - 沒有足夠的繪圖顏色,python

下面是腳本:

locerror_2d=[Scan_Around[1],Triangle_Around[1],M_shape_Around[1],Hilbert_Around[1],Scan_SbS[1],Triangle_SbS[1],M_shape_SbS[1],Hilbert_SbS[1]] 


# N = len(locerror_2d[0]) #same for all (here, I hope so...) 
# N1=len(locerror_2d[2]) 
H_cent,h_cent1 = np.histogram(locerror_2d[0], bins = 10, normed = True) # Random Walk Centroid 
hy_cent = np.cumsum(H_cent)*(h_cent1[1] - h_cent1[0]) 

H_1st,h_1st = np.histogram(locerror_2d[1], bins = 10, normed = True) # Random Walk Weighterd 
hy_1st = np.cumsum(H_1st)*(h_1st[1] - h_1st[0]) 

H_2nd,h_2nd = np.histogram(locerror_2d[2], bins = 10, normed = True) # Circle Walk Centroid 
hy_2nd = np.cumsum(H_2nd)*(h_2nd[1] - h_2nd[0]) 

H_3rd,h_3rd = np.histogram(locerror_2d[3], bins = 10, normed = True) # Circle Walk Weighterd 
hy_3rd = np.cumsum(H_3rd)*(h_3rd[1] - h_3rd[0]) 

H_mm,h_mm = np.histogram(locerror_2d[4], bins = 10, normed = True) # G Walk Centroid 
hy_mm = np.cumsum(H_mm)*(h_mm[1] - h_mm[0]) 

H_shr,h_shr = np.histogram(locerror_2d[5], bins = 10, normed = True) # G Walk Weighterd 
hy_shr = np.cumsum(H_shr)*(h_shr[1] - h_shr[0]) 

H_s,h_s = np.histogram(locerror_2d[6], bins = 10, normed = True) # G Walk Weighterd 
hy_s = np.cumsum(H_s)*(h_s[1] - h_s[0]) 

H_sh,h_sh = np.histogram(locerror_2d[7], bins = 10, normed = True) # G Walk Weighterd 
hy_sh = np.cumsum(H_sh)*(h_sh[1] - h_sh[0]) 


plt.hold(True) 
ddd_hist_cent, = plt.plot(h_cent1[1:], hy_cent,label="Scan_Around")  # centroid 
ddd_hist_1st, = plt.plot(h_1st[1:], hy_1st,label='Triangle_Around')  #Gradient 
ddd_circ_cent, = plt.plot(h_2nd[1:], hy_cent,label="M_shape_around")  # centroid 
ddd_circ_wei, = plt.plot(h_3rd[1:], hy_1st,label='Hilbert_Around')  #Gradient 
ddd_g_cent, = plt.plot(h_mm[1:], hy_cent,label="Scan_SbS")  # centroid 
ddd_g_wei, = plt.plot(h_shr[1:], hy_1st,label='Triangle_SbS')  #Gradient 
ddd_g_w, = plt.plot(h_s[1:], hy_cent,label='M_shape_SbS') 
ddd_g_we, = plt.plot(h_sh[1:], hy_1st,label='Hilbert_SbS') 

plt.hold(False) 

plt.rc('legend',**{'fontsize':10}) 
plt.legend(handles=[ddd_hist_cent, ddd_hist_1st, ddd_circ_cent, ddd_circ_wei, ddd_g_cent,ddd_g_wei, ddd_g_w],loc='center left', bbox_to_anchor=(0.75, 0.18)) #no trilateration here 
plt.ylabel('Probability') 
plt.xlabel('Localization Error, m') 
plt.ylim(ymax = 1.1, ymin = 0) 
plt.title('Path Planning Algorithms') 
plt.grid() 
plt.show() 

謝謝

回答

2

我喜歡從顏色表使用此代碼

def getColor(c, N, idx): 
    import matplotlib as mpl 
    cmap = mpl.cm.get_cmap(c) 
    norm = mpl.colors.Normalize(vmin=0.0, vmax=N - 1) 
    return cmap(norm(idx)) 

這裏直接讀取我的色彩,c是的名稱顏色地圖(請參閱https://matplotlib.org/examples/color/colormaps_reference.html的列表),N是您想要的顏色總數,idx是隻是一個會產生特定顏色的索引。

然後當調用繪圖功能時,只需添加color=getColor(c, N, idx)選項。

1

好的。我知道了。在劇情結束時,我只需要顯示顏色。

ddd_hist_cent, = plt.plot(h_cent1[1:], hy_cent,label="Scan_Around", c='yellow') 
1

最簡單的解決方案:給最後的曲線不同的顏色:

plt.plot(h_sh[1:], hy_1st,label='Hilbert_SbS', color="orange") 

Matplotlib版本1.5或以下在其顏色週期7種不同的顏色,而matplotlib 2.0有10點不同的顏色。因此,更新matplotlib是另一種選擇。

一般來說,你當然可以定義你自己的顏色循環,它有你想要的顏色。

  • 生成從顏色表循環儀,如在this question所示:

    import matplotlib.pyplot as plt 
    from cycler import cycler 
    import numpy as np 
    
    N = 8 # number of colors 
    plt.rcParams["axes.prop_cycle"] = cycler('color', plt.cm.jet(np.linspace(0,1,N))) 
    
  • 建立從顏色列表循環儀:

    import matplotlib.pyplot as plt 
    from cycler import cycler 
    
    colors=["aquamarine","crimson","gold","indigo", 
         "lime","orange","orchid","sienna"] 
    plt.rcParams["axes.prop_cycle"] = cycler('color',colors)