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

  • SEARCH
  • Home
  • 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 7645301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:54:25+00:00 2026-05-31T09:54:25+00:00

I have a simple image that I’m showing with imshow in matplotlib. I’d like

  • 0

I have a simple image that I’m showing with imshow in matplotlib. I’d like to apply a custom colormap so that values between 0-5 are white, 5-10 are red (very simple colors), etc. I’ve tried following this tutorial:

http://assorted-experience.blogspot.com/2007/07/custom-colormaps.html with the following code:

cdict = {
'red'  :  ((0., 0., 0.), (0.5, 0.25, 0.25), (1., 1., 1.)),
'green':  ((0., 1., 1.), (0.7, 0.0, 0.5), (1., 1., 1.)),
'blue' :  ((0., 1., 1.), (0.5, 0.0, 0.0), (1., 1., 1.))
}

my_cmap = mpl.colors.LinearSegmentedColormap('my_colormap', cdict, 3)

plt.imshow(num_stars, extent=(min(x), max(x), min(y), max(y)), cmap=my_cmap)
plt.show()

But this ends up showing strange colors, and I only need 3-4 colors that I want to define. How do I do this?

  • 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-05-31T09:54:26+00:00Added an answer on May 31, 2026 at 9:54 am

    You can use a ListedColormap to specify the white and red as the only colors in the color map, and the bounds determine where the transition is from one color to the next:

    import matplotlib.pyplot as plt
    from matplotlib import colors
    import numpy as np
    
    np.random.seed(101)
    zvals = np.random.rand(100, 100) * 10
    
    # make a color map of fixed colors
    cmap = colors.ListedColormap(['white', 'red'])
    bounds=[0,5,10]
    norm = colors.BoundaryNorm(bounds, cmap.N)
    
    # tell imshow about color map so that only set colors are used
    img = plt.imshow(zvals, interpolation='nearest', origin='lower',
                        cmap=cmap, norm=norm)
    
    # make a color bar
    plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[0, 5, 10])
    
    plt.savefig('redwhite.png')
    plt.show()
    

    The resulting figure has only two colors:

    enter image description here

    I proposed essentially the same thing for a somewhat different question: 2D grid data visualization in Python

    The solution is inspired by a matplotlib example. The example explains that the bounds must be one more than the number of colors used.

    The BoundaryNorm is a normalization that maps a series of values to integers, which are then used to assign the corresponding colors. cmap.N, in the example above, just defines the number of colors.

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

Sidebar

Related Questions

I have a simple image upload script that uses SimpleImage.php (http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/) to resize and
I have a simple Button control that contains an Image object as its content.
I'm trying to build the simple layout that I have got in the image
I have a simple jQuery image switch that is failing. $('.heart_img').click(function() { var heart_type
I have a simple image inside an HTML form that acts as a button.
I have coded a simple div containing an image that follows the mouse cursor
I have a simple image that stores the width, height, pixelformat, and some other
i have simple url of image, i want to display that image in Bitmap
I have a simple image gallery that I've paginated, and I want to to
I have programmed a simple app that every X minutes checks if an image

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.