0

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.

enter image description here

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()

1 Answers1

1

you need to generate color based on the y value, so you should take the y value and use it to interpolate the color's R,G,B values separately using np.interp

import numpy as np
import matplotlib.pyplot as plt

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)]

color1 = "#EB1410"
color2 = "#080606"
def gradient_cmap(c):
    c1_rgb = np.array(hex_to_RGB(color1)) / 255
    c2_rgb = np.array(hex_to_RGB(color2)) / 255
    R_map = [c1_rgb[0],c2_rgb[0]]
    G_map = [c1_rgb[1],c2_rgb[1]]
    B_map = [c1_rgb[2],c2_rgb[2]]
    in_val = [np.amin(c),np.amax(c)]
    R = np.interp(c,in_val,R_map).reshape([-1,1])
    G = np.interp(c,in_val,G_map).reshape([-1,1])
    B = np.interp(c,in_val,B_map).reshape([-1,1])
    return np.hstack([R,G,B])

plt.figure(figsize=(12, 7))
plt.scatter(x, y, c=gradient_cmap(y))
plt.show()

enter image description here

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • Hi @Ahmed AEK Thank you for response. This is an interesting answer. Can we do this using line plot? I wanted to do this using line plot? Is it possible? – ankush jamthikar Jan 07 '23 at 03:25
  • @ankushjamthikar check this https://stackoverflow.com/questions/8500700/how-to-plot-a-gradient-color-line-in-matplotlib – Ahmed AEK Jan 07 '23 at 07:05