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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:12:28+00:00 2026-06-03T05:12:28+00:00

I want to show a matrix in color plot and give specific colors to

  • 0

I want to show a matrix in color plot and give specific colors to two or more special values.

import numpy as np
from pylab import *

np.random.seed(10)    
a=np.random.randint(-1,10, size=(5, 5))
print a

fig, ax = plt.subplots()
mat=ax.matshow(a, cmap=cm.jet, vmin=1, vmax=10)
colorbar(mat)
show()

Here are the values of a matrix:

[[ 8  3 -1  0  8]
 [-1  0  9  7  8]
 [-1  9  7  5  3]
 [ 2 -1  3  5  7]
 [ 9  0  7  3  0]]

Here is the plot:
enter image description here
I’d like to assign black color to all -1 entries and white to all 0 entries, and I’d like it to be shown on color bar below number one as two discrete colors. Here is an example, my photo editing skills are poor, but it should be clear what I want (color bar should be in scale):
enter image description here
It’s not important to me to have a continuous jet color map, I’d be satisfied with a solution where my color bar would be discrete, and consisted of, for example 10 colors, of which two would be black and white, 8 from jet color map colors. However, it’s important that -1 and 0 have distinct colors, no matter what the total range of values is.
For example if value range was from -1 to 1000:
enter image description here

  • 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-03T05:12:30+00:00Added an answer on June 3, 2026 at 5:12 am

    You can use ListedColormap:

    import numpy as np
    import matplotlib as mpl
    from matplotlib import pyplot as plt
    
    N = 10
    
    np.random.seed(10)    
    a=np.random.randint(-1, N, size=(5, 5))
    print a
    
    fig, ax = plt.subplots()
    
    colors = [(0.0,0.0,0.0),(1.0,1.0,1.0)]
    colors.extend(mpl.cm.jet(np.linspace(0, 1, N-1)))
    cmap = mpl.colors.ListedColormap(colors)
    mat=ax.matshow(a, cmap=cmap, vmin=-1, vmax=N-1)
    cax = plt.colorbar(mat, ticks=np.linspace(-0.5, N-1-0.5, N+1))
    cax.set_ticklabels(range(-1, N))
    plt.show()
    

    enter image description here

    for value ranges, you can convert the values to range index first.

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

Sidebar

Related Questions

I am plotting a confusion matrix with matplotlib with the following code: from numpy
I want to plot a confusion matrix using Pylab. The class labels along the
I have a small map and I want to show two different information categories
This is another simple 'matrix' question in Mathematica. I want to show how I
Continuing with my matrix multiplication question , I want to show the following expression
I want show an information page after success db opartions. And after the info
I want show annotation view (above one of my pin) after load map. For
hii I want show a pop up Dialog box when i click on to
I'm using Drupal 7 and Views module in my site. And want show number
I want to show array objects at the same time in different 10 labels.

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.