Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8043865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:03:09+00:00 2026-06-05T05:03:09+00:00

I have a certain array of floats (in Python) that might range from 0

  • 0

I have a certain array of floats (in Python) that might range from 0 to 100. I want to create a pseudo-color image so that the colors vary from green (corresponding to 0) to red (100). This is similar to pcolor from matplotlib. However, I do not want to use pcolor.

Is there a function like pseudocolorForValue(val,(minval,maxval)) which returns an RGB triple corresponding to the pseudo-color value for val? Also, is there a flexibility in this function to choose whether to display colors from green-to-red or from red-to-green?

Thanks,
Nik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:03:10+00:00Added an answer on June 5, 2026 at 5:03 am

    You could write your own function that converted the values 0…100 → 0…120 degrees and then used that value as the H (or angle) of a color in the HSV (or HLS) colorspace. This could then be converted into an RGB color for display purposes. Linearly interpreted colors often look better when they’re calculated in this colorspace: Here’s what HSV colorspace looks like:

    hsv colorspace diagram

    Update:

    Good news, I was pleasantly surprised to discover that Python has colorspace conversion routines in its built-in colorsys module (they really mean “batteries included”). What’s nice about that is that it makes creating a function that does what I described fairly easy, as illustrated below:

    from colorsys import hsv_to_rgb
    
    def pseudocolor(val, minval, maxval):
        """ Convert val in range minval..maxval to the range 0..120 degrees which
            correspond to the colors Red and Green in the HSV colorspace.
        """
        h = (float(val-minval) / (maxval-minval)) * 120
    
        # Convert hsv color (h,1,1) to its rgb equivalent.
        # Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
        r, g, b = hsv_to_rgb(h/360, 1., 1.)
        return r, g, b
    
    if __name__ == '__main__':
        steps = 10
    
        print('val       R      G      B')
        for val in range(0, 100+steps, steps):
            print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
                                                    val, *pseudocolor(val, 0, 100)))
    

    Output:

    val       R      G      B
      0 -> (1.000, 0.000, 0.000)
     10 -> (1.000, 0.200, 0.000)
     20 -> (1.000, 0.400, 0.000)
     30 -> (1.000, 0.600, 0.000)
     40 -> (1.000, 0.800, 0.000)
     50 -> (1.000, 1.000, 0.000)
     60 -> (0.800, 1.000, 0.000)
     70 -> (0.600, 1.000, 0.000)
     80 -> (0.400, 1.000, 0.000)
     90 -> (0.200, 1.000, 0.000)
    100 -> (0.000, 1.000, 0.000)
    

    Here’s a sample showing what its output looks like:

    sample showing color interpolation in HSV colorspace

    I think you may find the colors generated nicer than in my other answer.

    Generalizing:

    It’s possible to modify this function to be a little more generic in the sense that it will work with colors other then just the Red and Green currently hardcoded into it.

    Here’s how to do that:

    def pseudocolor(val, minval, maxval, start_hue, stop_hue):
        """ Convert val in range minval..maxval to the range start_hue..stop_hue
            degrees in the HSV colorspace.
        """
        h = (float(val-minval) / (maxval-minval)) * (stop_hue-start_hue) + start_hue
    
        # Convert hsv color (h,1,1) to its rgb equivalent.
        # Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
        r, g, b = hsv_to_rgb(h/360, 1., 1.)
        return r, g, b
    
    if __name__ == '__main__':
        # angles of common colors in hsv colorspace
        RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA = range(0, 360, 60)
        steps = 10
    
        print('val       R      G      B')
        for val in range(0, 100+steps, steps):
            print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
                    val, *pseudocolor(val, 0, 100, YELLOW, BLUE)))
    

    Results:

    sample showing color interpolation in HSV colorspace between yellow and blue

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a certain session variable that is actually an array. I want to
I have the following loop that adds an object to an array if certain
I have an array of a certain type. Now I want to find an
I have to filter out certain elements from an array in javascript and thought
I have a ParameterInfo array. I need to remove certain values from the array.
Background: I have an N-length array of positive random numbers that are certain to
Hey, I have an array of strings and I want to replace a certain
I have a json array with certain objects : first name , last name,
I have this given array of ids assigned to a certain variable, $post_id =
I have an array / list of numbers. Each number has a certain priority

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.