2

I want to get a plot similar to the following plot that has different colors based on values for x-axis. Ignore the u and f letters and also the blue curve and gray lines. I only need the green and red lines. So, if you use my code, you will get a plot that is all one color. What I want is to have different color when x is between 0 and the turning point (in this case it is x=50%) and then a different color for the rest.

Code:

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(['Reg-ALSRank@10','Equity-Line'], loc='upper left',prop={'size':16})
plt.xlabel('% of items ')
plt.ylabel('% of times being recommended')
plt.show()
8
  • You have to plot two lines. Commented Feb 22, 2017 at 19:00
  • It is two lines in this case, but what if it is a curve that should be different colors for different intervals. I don't think drawing multiple lines is the solution to be honest. Commented Feb 22, 2017 at 19:02
  • To be honest, drawing 2 lines (one for each color) is the solution. I'm currently having difficulties to understand which is the point at which you want to change the color. Do you determine this point in your script somewhere? Commented Feb 22, 2017 at 19:05
  • No that point is when the curve has a turn and it is not clear what the point is until you call the GRLC function. Commented Feb 22, 2017 at 19:07
  • But sure imagine you know where the point is. How does that can help? Commented Feb 22, 2017 at 19:08

1 Answer 1

3

This is how you would plot two lines of different colors, knowing the index in the array at which the color should change.

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

This works because by default, subsequent line plots have a different color, but you could of course set the color yourself,

plt.plot(x[:x0+1], y[:x0+1], color="cornflowerblue")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.