2017-02-22 9 views
0

x軸の値に基づいて異なる色を持つ次のプロットに似たプロットを取得します。 uとfの文字を無視し、青い曲線と灰色の線も無視します。私は緑と赤の線だけが必要です。だから、私のコードを使用すると、すべて1色のプロットが得られます。私が望むのは、xが0と変わり点(この場合はx=50%)の間にあるときに異なる色を持ち、残りの色が違うということです。x軸の値に基づいて複数の色を持つMatplotlibプロット

コード:

import matplotlib.pyplot as plt 

def GRLC(values): 
    n = len(values) 
    assert(n > 0), 'Empty list of values' 
    sortedValues = sorted(values) #Sort smallest to largest 

    #Find cumulative totals 
    cumm = [0] 
    for i in range(n): 
     cumm.append(sum(sortedValues[0:(i + 1)])) 

    #Calculate Lorenz points 
    LorenzPoints = [[], []] 
    sumYs = 0   #Some of all y values 
    robinHoodIdx = -1 #Robin Hood index max(x_i, y_i) 
    for i in range(1, n + 2): 
     x = 100.0 * (i - 1)/n 
     y = 100.0 * (cumm[i - 1]/float(cumm[n])) 
     LorenzPoints[0].append(x) 
     LorenzPoints[1].append(y) 
     sumYs += y 
     maxX_Y = x - y 
     if maxX_Y > robinHoodIdx: robinHoodIdx = maxX_Y 

    giniIdx = 100 + (100 - 2 * sumYs)/n #Gini index 

    return [giniIdx, giniIdx/100, robinHoodIdx, LorenzPoints] 

reg=[400,200] 
result_reg = GRLC(reg) 
print 'Gini Index Reg', result_reg[0] 
print 'Gini Coefficient Reg', result_reg[1] 
print 'Robin Hood Index Reg', result_reg[2] 

#Plot 
plt.plot(result_reg[3][0], result_reg[3][1], [0, 100], [0, 100], '--') 

plt.legend(['[email protected]','Equity-Line'], loc='upper left',prop={'size':16}) 
plt.xlabel('% of items ') 
plt.ylabel('% of times being recommended') 
plt.show() 
+0

次の2本のラインをプロットする必要があるためこれは動作します。 – Goyo

+0

この場合は2行ですが、異なる間隔で異なる色にする必要がある曲線の場合はどうなりますか?私は複数の線を描くことが正直な解決策だとは思わない。 – HimanAB

+0

正直に言うと、2行(各色に1つ)を描く**は**解決策です。私は現在、あなたが色を変えたい点が何であるかを理解するのが難しいです。あなたはどこかでスクリプトのこの点を判断していますか? – ImportanceOfBeingErnest

答えて

1

これはあなたが色が変わるべきで配列のインデックスを知って、異なる色の2行をプロットする方法をです。

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0,49, num=50) 
y = x**2 

x0 = 23 

plt.plot(x[:x0+1], y[:x0+1]) 
plt.plot(x[x0:], y[x0:]) 
plt.show() 

enter image description here

デフォルトでは、その後のラインプロットは、別の色を持っていますが、あなたはもちろん、色を自分で設定することができ、

plt.plot(x[:x0+1], y[:x0+1], color="cornflowerblue") 
関連する問題