Matlab, Python: Fixing colormap to specified values

It is a simple but common task required when trying to fix a colormap according to a 2D matrix of values. To demonstrate consider the problem in Matlab, the solution does not need to be in Matlab (i.e., the code presented here is only for demonstration purpose).

x = [0,1,2; 3,4,5; 6,7,8]; imagesc(x) axis square axis off

So the output is as:
Matlab, Python: Fixing colormap to specified values

when some values change to over the maximum value it happens like:

x = [0,1,2; 3,4,5; 6,7,18];

which looks logical but makes problems when we wish to compare/trace elements in two maps. Since the colormap association is changed it is almost impossible to find an individual cell for comparison/trace etc. Matlab, Python: Fixing colormap to specified values

The solution I implemented is to mask the matrix as:

x = [0,1,2; 3,4,5; 6,7,18]; m = 8; x(x>=m) = m;

which works perfectly.
Since the provided code requires searching/filtering (extra time consuming!) I wonder if there is a general/more efficient way for this job to be implemented in Matlab, Python etc?

One of the cases that this issue occurs is when we have many simulations sequentially and wish to make a sense-making animation of the progress; in this case each color should keep its association fixed.

-------------Problems Reply------------

The indexing is pretty quick so I don't think you need worry.

However, in Matlab, you can pass in the clims argument to imagesc:

imagesc(x,[0 8]);

This maps all values above 8 to the top colour in the colour scale, and all values below 0 to the bottom colour in the colour scale, and then stretches the scale for colours in-between.

imagesc documentation.

In Python using package MatPlotLib the solution is as follows:

import pylab as pl
x = [[0,1,2],[3,4,5],[6,7,18]]
pl.matshow(x, vmin=0, vmax=8)

So vmin and vmax are boundary limits for the full range of colormap.

f1 = figure;
x = [0,1,2; 3,4,5; 6,7,8];
axis square
axis off

limits = get(gca(f1),'CLim');

f2 = figure;
z = [0,1,2; 3,4,5; 6,7,18];
axis square
axis off

Category:python Views:1516 Time:2012-01-30

Related post

  • How can I cast variables in matlab with fixed floating point 2012-02-16

    Is it possible convert a double variable to a float (single in Matlab) with fixed floating point? For example x = 10.023213032130123021302130210331232132103312321 to x = 10.0231 Thank you ! --------------Solutions------------- First convert to single

  • Python Fixed Length Packet 2009-10-05

    I am trying to build a fixed length packet in python for an ATSC PSIP generator. This is probably very simple but so far I can't seem to get it to work. I am trying to build a packet with fields similar to the following: table_id = 0xCB syntax = 0b1

  • Matlab question: fixing the scale of Y axis in hist 2011-07-12

    I want to draw several histograms. But I want the Y axis to be fixed, like from 1000 to 1 by 100. How can I specify them。 Please advise. --------------Solutions------------- Consider this example: %# some data X = randn(1000,3); nbins = 10; %# comput

  • Python fixed width strings with the Template object 2011-08-20

    Is there a way make fixed width substitutions with the python String.Template objects substitution language For example if I wanted s to be 'A string 0003' how would I modify this code: from string import Template stmpl=Template("A string ${tcount}")

  • MATLAB + Python Ideas? 2011-11-23

    I have been noticing a lot of necessary functions that are present in MATLAB but seem to not exist in Python. I am planning to make a Package or Master Class containing all of these functions and was wondering on any specifics Y'all might have or wou

  • Python - fixed exponent in scientific notation? 2011-11-24

    Consider the following Python snippet: for ix in [0.02, 0.2, 2, 20, 200, 2000]: iss=str(ix) + "e9" isf=float(iss) print(iss + "\t=> " + ("%04.03e" % isf ) + " (" + str(isf) + ")") It generates the following output: 0.02e9 => 2.000e+07 (20000000

  • How to vary the line color of a matlab plot (like colormap)? 2011-12-19

    I have a 2D space in which a function value is defined (you can think of it as a manifold). Now I plotted the function value using contourf and changed the colormap to something softer than jet. So far it looks quite good. Now I want to draw a line r

  • When saving a figure as eps file, Matlab cuts off colormap labels 2012-01-03

    I have a figure generated using contourf with a colorbar. Most of my plots are fine, but when the values on the colorbar are of the order 10^{-3}, either the numbers 0.005 etc are written by the colorbar, or x10^{-3} is written at the top. In both ca

  • Python Matplotlib Colormap 2012-03-27

    I use the colormap "jet" to plot my graphics. But, I would like to have the lower values in white color and this colormap goes from blue to red colors. I also don't want to use another colormap because I need this range of colors... I tried to make m

  • Call Python function from MATLAB 2009-11-10

    I need to call a Python function from MATLAB. how can I do this? --------------Solutions------------- I had a similar requirement on my system and this was my solution: In MATLAB there is a function called perl.m, which allows you to call perl script

  • Scientific computing in Python for MATLAB programmers 2011-08-02

    I was wondering if anybody knows of a good tutorial or introductory text on scientific computing on Python/SciPy for MATLAB programmers. I was thinking of something along the lines of David Hiebeler's text on R for MATLAB programmers, which provides

  • export matlab variable to text for python usage 2011-08-19

    So let's start off by saying I'm a total beginner in matlab. I'm working with python and now I've recieved some data in a matlab file that I need to export to a format I could use with python. I've googled around and found I can export a matlab varia

  • Fixed point in Matlab 2012-03-06

    Can someone please explain this? As I understand it provides less precision..Is it a speed-up that one wishes to get by using it? When is it good to use? Should I use it in Matlab Coder? --------------Solutions------------- Not all the computers in t

  • how to show points on image in matlab? 2010-10-01

    i have some pixel points lets say p1(1,1) and p2(1,10).......and so on i want to display these points on image in any color. how to do this? --------------Solutions------------- MATLAB plot documentation is pretty comprehensive. LineSpec properties l

  • What can be done about "The command is too long to execute" error in MATLAB? 2011-06-09

    I am calling a Python program from MATLAB and passing an array to the program. I am writing the following lines in MATLAB workspace: % Let us assume some random array num1 = ones(1,100); % I am forced to pass parameters as string due to the MATLAB-Py

  • Navigating in the python terminal 2009-10-23

    I want a 64-bit python interpreter on my Mac so I had to rebuild from source. However, with my own custom build interpreter I run into issues when I try to navigate when I run the interpreter from inside a shell. Typing python into the bash shell res

  • Suggestions to learn any programming language after Matlab 2010-07-07

    I've taken a course in C++ in my freshman year in undergrad. However once it was over I never employed it in any project or use. I am familiar with some average topics in C++ prior to where 'link list' starts. Once I got the opportunity to do my firs

  • Python: MySQLdb and "Library not loaded: libmysqlclient.16.dylib" 2010-12-30

    The setup... Trying to set up a clean Mac os X 10.6 install to develop python/django and I didn't remember running into this on 10.5. After installing MySQL from the installer on mysql-5.5.8-osx10.6-x86_64.dmg I ran $ sudo pip install MySQL-python an

  • Reformat a Python file to have 4 space indentations 2011-03-07

    I'm editing a Python file that uses two spaces for programmatic indents - I prefer 4 spaces. In my .vimrc I have the following settings related to indentation: set tabstop=4 "Indentation levels every four columns set expandtab "Convert all tabs typed

Copyright (C), All Rights Reserved.

processed in 0.091 (s). 11 q(s)