I am working on a task where I need to generate a line plot with maximum values assigned to one color and minimum values assigned to another. There should be gradient colors between the maximum and minimum values.
To generate a line plot with gradient colour, I use the code previously provided. As you can see, the output looks like this.
I saw the answer provided in enter link description here, but it is for step plot. I want to implement this using matplotlib line plot.
I would like the gradient colour to follow the y-values (i.e., from maximum to minimum) not the x-values (i.e., from left to right). I hope my question is clear. To generate the current output, I used the code below as a reference.
x = np.linspace(0, 4.*np.pi, 1000)
y = np.sin(x)
def hex_to_RGB(hex_str):
""" #FFFFFF -> [255,255,255]"""
#Pass 16 to the integer function for change of base
return [int(hex_str[i:i+2], 16) for i in range(1,6,2)]
def get_color_gradient(c1, c2, n):
"""
Given two hex colors, returns a color gradient
with n colors.
"""
assert n > 1
c1_rgb = np.array(hex_to_RGB(c1))/255
c2_rgb = np.array(hex_to_RGB(c2))/255
mix_pcts = [x/(n-1) for x in range(n)]
rgb_colors = [((1-mix)*c1_rgb + (mix*c2_rgb)) for mix in mix_pcts]
return ["#" + "".join([format(int(round(val*255)), "02x") for val in item]) for item in rgb_colors]
color1 = "#EB1410"
color2 = "#080606"
plt.figure(figsize=(12,7))
plt.scatter(x, y, color = get_color_gradient(color1, color2, len(x)))
plt.show()