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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:32:21+00:00 2026-06-12T23:32:21+00:00

In my application I’m transitioning from R to native Python (scipy + matplotlib) where

  • 0

In my application I’m transitioning from R to native Python (scipy + matplotlib) where possible, and one of the biggest tasks was converting from a R heatmap to a matplotlib heatmap. This post guided me with the porting. While most of it was painless, I’m still not convinced on the colormap.

Before showing code, an explanation: in the R code I defined “breaks”, i.e. a fixed number of points starting from the lowest value up to 10, and ideally centered on the median value of the data. Its equivalent here would be with numpy.linspace:

# Matrix is a DataFrame object from pandas
import numpy as np

data_min = min(matrix.min(skipna=True))
data_max = max(matrix.max(skipna=True))
median_value = np.median(matrix.median(skipna=True))

range_min = np.linspace(0, median_value, 50)
range_max = np.linspace(median_value, data_max, 50)
breaks = np.concatenate((range_min, range_max))

This gives us 100 points that will be used for coloring. However, I’m not sure on how to do the exact same thing in Python. Currently I have:

def red_black_green():
    cdict = {
       'red': ((0.0, 0.0, 0.0),
               (0.5, 0.0, 0.0),
               (1.0, 1.0, 1.0)),
       'blue': ((0.0, 0.0, 0.0),
                (1.0, 0.0, 0.0)),
       'green': ((0.0, 0.0, 1.0),
                 (0.5, 0.0, 0.0),
                 (1.0, 0.0, 0.0))
       }

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

    return my_cmap

And further down I do:

# Note: vmin and vmax are the maximum and the minimum of the data

# Adjust the max and min to scale these colors
if vmin > 0:
    norm = mpl.colors.Normalize(vmin=0, vmax=vmax / 1.08)
else:
    norm = mpl.colors.Normalize(vmin / 2, vmax / 2)

The numbers are totally empirical, that’s why I want to change this into something more robust. How can I normalize my color map basing on the median, or do I need normalization at all?

  • 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-12T23:32:23+00:00Added an answer on June 12, 2026 at 11:32 pm

    By default, matplotlib will normalise the colormap such that the maximum colormap value will be the maximum of your data. Likewise for the minimum of your data. This means that the median of the colormap (the middle value) will line up with the interpolated median of your data (interpolated if you don’t have a data point exactly at the median).

    Here’s an example:

    from numpy.random import rand
    import matplotlib.pyplot as plt
    import matplotlib.colors as mcolors
    
    cdict = {'red':   ((0.0, 0.0, 0.0),
                       (0.5, 0.0, 0.0),
                       (1.0, 1.0, 1.0)),
             'blue':  ((0.0, 0.0, 0.0),
                       (1.0, 0.0, 0.0)),
             'green': ((0.0, 0.0, 1.0),
                       (0.5, 0.0, 0.0),
                       (1.0, 0.0, 0.0))}
    
    cmap = mcolors.LinearSegmentedColormap(
    'my_colormap', cdict, 100)
    
    ax = plt.subplot(111)
    im = ax.imshow(2*rand(20, 20) + 1.5, cmap=cmap)
    plt.colorbar(im)
    plt.show()
    

    Notice the middle of the colour bar takes value 2.5. This is the median of the data range: (min + max) / 2 = (1.5+3.5) / 2 = 2.5.

    Hope this helps.

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

Sidebar

Related Questions

Application.Run(form); Actually I tried to call this from my project. I got this exception.
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to
Application requests KML data through AJAX from server. This data is stored in javascript
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application names on Google Play don't need to be unique, and it's possible to
My application has one activity which starts two services but does not bind them.
Application stores configuration data in custom section of configuration file. This information is used
Application : I am working on one mid-large size application which will be used
Application: This is a workshop proposal system for a conference. A user can create
Application running in django, apache, python and mysql: I want to create global variable

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.