2017-02-22 3 views
0

x 축의 값에 따라 다른 색상을 갖는 다음 플롯과 유사한 플롯을 얻고 싶습니다. u와 f 글자와 파란색 곡선과 회색 선은 무시하십시오. 나는 녹색과 빨간색 선만 있으면 돼. 그래서, 당신이 내 코드를 사용한다면, 당신은 하나의 색깔 인 그림을 얻을 것입니다. 내가 원했던 것은 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

당신은 두 줄을 플롯합니다. – Goyo

+0

이 경우 두 줄이지만 다른 간격의 다른 색상이어야하는 곡선 인 경우에는 어떻게됩니까? 솔직히 말해서 여러 줄을 그리는 것이 해결책이라고 생각하지 않습니다. – HimanAB

+0

솔직하게 말하면 2 줄 (각 ​​색상 당 하나씩) **은 ** 해결책입니다. 나는 현재 당신이 색깔을 바꾸고 싶은 시점을 이해하는 데 어려움을 겪고있다. 스크립트에서이 지점을 어디 선가 결정합니까? – ImportanceOfBeingErnest

답변

1

이것은 당신이 색상을 변경해야하는 배열의 인덱스를 알고, 서로 다른 색깔의 두 라인을 그릴 것입니다 방법이다.

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") 
관련 문제