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 8419855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:43:49+00:00 2026-06-10T02:43:49+00:00

I have a 2D array that I’m plotting with imshow and I would like

  • 0

I have a 2D array that I’m plotting with imshow and I would like to have costums colors depending on the value of each pixel of my array. I’ll explain it with an example.

from pylab import *
from numpy import *

img = ones((5,5))
img[1][1] = 2

imshow(img,interpolation='nearest');colorbar()

If you ran this code you would see a red square in a blue background. The red square corresponds to the pixel [1][1] in img, while the other pixel are colored blue because they have a value of 1. What if I want the red square to be colored with a custom color?
Or more generally, if I have a 2D array like img in the example, how can I color pixel with the same value with a color I can choose.

I have found this page that explains how to generate a custom colorbar but that’s not useful: http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps

  • 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-10T02:43:51+00:00Added an answer on June 10, 2026 at 2:43 am

    That link you sent has the following:

    But, what if I think those colormaps are ugly? Well, just make your
    own using matplotlib.colors.LinearSegmentedColormap. First, create a
    script that will map the range (0,1) to values in the RGB spectrum. In
    this dictionary, you will have a series of tuples for each color
    ‘red’, ‘green’, and ‘blue’. The first elements in each of these color
    series needs to be ordered from 0 to 1, with arbitrary spacing
    inbetween. Now, consider (0.5, 1.0, 0.7) in the ‘red’ series below.
    This tuple says that at 0.5 in the range from (0,1) , interpolate from
    below to 1.0, and above from 0.7. Often, the second two values in each
    tuple will be the same, but using diferent values is helpful for
    putting breaks in your colormap. This is easier understand than might
    sound, as demonstrated by this simple script:

       1 from pylab import *
       2 cdict = {'red': ((0.0, 0.0, 0.0),
       3                  (0.5, 1.0, 0.7),
       4                  (1.0, 1.0, 1.0)),
       5          'green': ((0.0, 0.0, 0.0),
       6                    (0.5, 1.0, 0.0),
       7                    (1.0, 1.0, 1.0)),
       8          'blue': ((0.0, 0.0, 0.0),
       9                   (0.5, 1.0, 0.0),
      10                   (1.0, 0.5, 1.0))}
      11 my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)
      12 pcolor(rand(10,10),cmap=my_cmap)
      13 colorbar()
    

    Isn’t this exactly what you want?

    Here’s an example of how to do it with the image you provided:

    import matplotlib
    from matplotlib import pyplot as plt
    from pylab import *
    
    img = ones((5,5))
    img[1][1] = 2
    
    cdict = {'red': ((0.0, 0.0, 0.0),
                    (0.5, 1.0, 0.7),
                         (1.0, 1.0, 1.0)),
                 'green': ((0.0, 0.0, 0.0),
                           (0.5, 1.0, 0.0),
                           (1.0, 1.0, 1.0)),
                 'blue': ((0.0, 0.0, 0.0),
                          (0.5, 1.0, 0.0),
                         (1.0, 0.5, 1.0))}
    
    my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)
    plt.pcolor(img,cmap=my_cmap)
    plt.colorbar()
    plt.show()
    

    Also, if you really want to map a number to a colour you can use discrete_cmap as specified in that example you linked to, here’s the example method the scipy documentation provides:

    def discrete_cmap(N=8):
        """create a colormap with N (N<15) discrete colors and register it"""
        # define individual colors as hex values
        cpool = [ '#bd2309', '#bbb12d', '#1480fa', '#14fa2f', '#000000',
                  '#faf214', '#2edfea', '#ea2ec4', '#ea2e40', '#cdcdcd',
                  '#577a4d', '#2e46c0', '#f59422', '#219774', '#8086d9' ]
        cmap3 = col.ListedColormap(cpool[0:N], 'indexed')
        cm.register_cmap(cmap=cmap3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that I would like to sort, that should be easy,
I have an array that contains several arrays and I would like to order
I have an array that looks something like this: Array ( [0] => apple
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have an array that looks like the following: array = [[1, 5], [4,
I have an array that looks like this: ( { title = Chris; isMale
I have a multidimensional array that look like this: Array [1] => Array (
i have an array that looks like: array(5) { [title]=> string(22) http://example.com/288 [title2]=> string(22)
I have an Array that returns a requested series of dates that I would
I have an array that looks like this except with over 40 more Array

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.