Thread Subject: nonlinear colorbar

Subject: nonlinear colorbar

From: Irene

Date: 6 Oct, 2008 14:45:04

Message: 1 of 11

Dear all,

For a contourplot I would like to create a colorbar ranging from 0-5, having levels @ [0 0.001 0.005 0.01 0.05 1 2 3 4 5].
I manage to create the plot + colorbar having the right levels and labeled lines, but I do not know how to adress certain colors to each level. The way I do it, no matter which colomap I choose, colors of the lowest levels are to similar to see any difference among them. How do I create a nonlinear colorbar (avoiding to plot log10(data)) by specifying the colors manually?

Irene

Subject: nonlinear colorbar

From: Carlos Adrian Vargas Aguilera

Date: 6 Oct, 2008 18:33:03

Message: 2 of 11

try:

>> figure
>> x = [0 0.001 0.005 0.01 0.05 1 2 3 4 5];
>> clim = [min(x) max(x)];
>> dx = min(diff(x));
>> y = clim(1):dx:clim(2);
>> Nx = length(x);
>> cmap = colormap(jet(Nx));
>> cmap2 = [...
>> interp1(x(:),cmap(:,1),y(:)) ...
>> interp1(x(:),cmap(:,2),y(:)) ...
>> interp1(x(:),cmap(:,3),y(:)) ...
>> ];
>> S = peaks(30);
>> surf(S - min(S(:)))
>> colormap(cmap2)
>> caxis(clim)
>> colorbar

Carlos

Subject: nonlinear colorbar

From: Carlos Adrian Vargas Aguilera

Date: 6 Oct, 2008 19:37:02

Message: 3 of 11

Or this one is better:

>> figure
>> x = [0 0.001 0.005 0.01 0.05 1 2 3 4 5];
>> Nx = length(x);
>> clim = [min(x) max(x)];
>> dx = min(diff(x));
>> y = clim(1):dx:clim(2);
>> for k=1:Nx-1, y(y>x(k) & y<=x(k+1)) = x(k+1); end % NEW
>> cmap = colormap(jet(Nx));
>> cmap2 = [...
>> interp1(x(:),cmap(:,1),y(:)) ...
>> interp1(x(:),cmap(:,2),y(:)) ...
>> interp1(x(:),cmap(:,3),y(:)) ...
>> ];
>> S = peaks(30);
>> surf(S - min(S(:)))
>> colormap(cmap2)
>> caxis(clim)
>> colorbar
 
Carlos

Anyway, this is part of the utilities I sooner or later submit to the FileExchange

Subject: nonlinear colorbar

From: fburton@nyx.net (Francis Burton)

Date: 7 Oct, 2008 08:22:35

Message: 4 of 11

In article <gcdpcu$3c$1@fred.mathworks.com>,
Carlos Adrian Vargas Aguilera <nubeobscura@hotmail.com> wrote:
>[snip]
>>> colorbar
>
>Carlos
>
>Anyway, this is part of the utilities I sooner or later submit to the
>FileExchange

Excellent, Carlos - I look forward to that!

Btw, has anyone considered producing a "cookbook" document
(online or even printed paper) assembling these kind of neat
techniques? It also could include a selection of the more
frequently asked questions. I'm sure there would be a market
for such a document/book.

Francis

Subject: nonlinear colorbar

From: Carlos Adrian Vargas Aguilera

Date: 9 Oct, 2008 13:53:06

Message: 5 of 11

> Btw, has anyone considered producing a "cookbook" document
> (online or even printed paper) assembling these kind of neat
> techniques? It also could include a selection of the more
> frequently asked questions. I'm sure there would be a market
> for such a document/book.


The world-wide-web is never enough :)

I think that what you can do with (or for) MATLAB is that much that the fanctastic FileExchange and this FORUM are the answer and are better than any FAQ...

Anyway, I'm preparing some functions for colorbars and colormaps for the FileExchange but is taking me a while. For example, now I'm working on how to put logaritmic scale in the not-to-friendly MATLAB's colorbar!!! and as always, DIGGING IN!

Carlos

Subject: nonlinear colorbar

From: fburton@nyx.net (Francis Burton)

Date: 9 Oct, 2008 20:03:04

Message: 6 of 11

In article <gcl2c2$ch$1@fred.mathworks.com>,
Carlos Adrian Vargas Aguilera <nubeobscura@hotmail.com> wrote:
>> Btw, has anyone considered producing a "cookbook" document
>> (online or even printed paper) assembling these kind of neat
>> techniques? It also could include a selection of the more
>> frequently asked questions. I'm sure there would be a market
>> for such a document/book.
>
>The world-wide-web is never enough :)

The world-wide-web is TOO MUCH - it needs a good editor! :)

>I think that what you can do with (or for) MATLAB is that much that the
>fanctastic FileExchange and this FORUM are the answer and are better
>than any FAQ...

Seriously, the trouble with this forum when it comes to FAQs is
that you don't know whether or not the answer to the question that
appears in the subject line is a new and possibly better one than
those you have seen so many times before, without actually reading
it. (And then you might discover the question doesn't match the
subject line, or is ill-posed) With a book, at least you would
have a pretty good idea that questions only get answered once in
a definitive way!

The quality control argument applies also to the FileExchange,
to some extent.

>Anyway, I'm preparing some functions for colorbars and colormaps for the
>FileExchange but is taking me a while. For example, now I'm working on
>how to put logaritmic scale in the not-to-friendly MATLAB's colorbar!!!
>and as always, DIGGING IN!

Sounds good!

Francis

Subject: nonlinear colorbar

From: Erwin

Date: 18 May, 2011 10:26:03

Message: 7 of 11

The function works, but does not exactly do what I want for non-logarithmic data.

I have a 2D matrix with values between -4 and +46. I want the negative values to be blue (-4) to black (0) and the positive values to be black (0) to red (+46). I want the center of the colorbar at 0 (not at 21) and hence as much bins in the negative part as in the positive part. How can I do this?

Is there a function to set the color limits not linear but to for instance:
CLim = [linspace(-4,0,128) linspace(0,46,128)];

fburton@nyx.net (Francis Burton) wrote in message <1223582584.295782@irys.nyx.net>...
> In article <gcl2c2$ch$1@fred.mathworks.com>,
> Carlos Adrian Vargas Aguilera <nubeobscura@hotmail.com> wrote:
> >> Btw, has anyone considered producing a "cookbook" document
> >> (online or even printed paper) assembling these kind of neat
> >> techniques? It also could include a selection of the more
> >> frequently asked questions. I'm sure there would be a market
> >> for such a document/book.
> >
> >The world-wide-web is never enough :)
>
> The world-wide-web is TOO MUCH - it needs a good editor! :)
>
> >I think that what you can do with (or for) MATLAB is that much that the
> >fanctastic FileExchange and this FORUM are the answer and are better
> >than any FAQ...
>
> Seriously, the trouble with this forum when it comes to FAQs is
> that you don't know whether or not the answer to the question that
> appears in the subject line is a new and possibly better one than
> those you have seen so many times before, without actually reading
> it. (And then you might discover the question doesn't match the
> subject line, or is ill-posed) With a book, at least you would
> have a pretty good idea that questions only get answered once in
> a definitive way!
>
> The quality control argument applies also to the FileExchange,
> to some extent.
>
> >Anyway, I'm preparing some functions for colorbars and colormaps for the
> >FileExchange but is taking me a while. For example, now I'm working on
> >how to put logaritmic scale in the not-to-friendly MATLAB's colorbar!!!
> >and as always, DIGGING IN!
>
> Sounds good!
>
> Francis

Subject: nonlinear colorbar

From: ImageAnalyst

Date: 18 May, 2011 12:14:27

Message: 8 of 11

On May 18, 6:26 am, "Erwin " <e.t.r.ros...@tue.nl> wrote:
> The function works, but does not exactly do what I want for non-logarithmic data.
>
> I have a 2D matrix with values between -4 and +46. I want the negative values to be blue (-4) to black (0) and the positive values to be black (0) to red (+46). I want the center of the colorbar at 0 (not at 21) and hence as much bins in the negative part as in the positive part. How can I do this?
>
> Is there a function to set the color limits not linear but to for instance:
> CLim = [linspace(-4,0,128) linspace(0,46,128)];
---------------------------------------------------------------------------------------------------------
This doesn't make sense. A colormap is a correspondence between
scalar values and colors. It is neither linear or non-linear. You
simply need to decide how many colors to display - this is commonly
256 since we use 24 bit (8 bit RGB) displays, but often MATLAB uses
only 64 colors to span the 256 values. Then you just create your
custom colormap. Figure out what the RGB values should be and assign
them, kind of like this:

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Create sample data in the range -4 to +46
grayImage = 50 * rand(400,400)-4;
% Display it
imshow(grayImage);

% Enlarge figure to nearly screen.
screenSize = get(0,'Screensize');
figSize = screenSize;
figSize(2) = screenSize(4) * 0.1
figSize(4) = screenSize(4) * 0.8
set(gcf, 'Position', figSize);

% Create colormap
cmap = zeros(256,3); % Initialize
zeroPoint = round(256 * (4/50));
cmap(1:zeroPoint, 1) = linspace(1,0,zeroPoint); % Red -4 to 0
cmap(zeroPoint+1:end, 3) = linspace(0,1,(256-zeroPoint)) % Blue from 0
to 46

% Pseudocolor it.
colormap(cmap);
% IMPORTANT NOTE: Play around with this to get the tick labels
right...
colorbar('YTickLabel', {'-4','46'})

Subject: nonlinear colorbar

From: Erwin

Date: 18 May, 2011 15:34:04

Message: 9 of 11

Thanks for your answer. Alas, it does not what I want. It makes a colormap with 20 bins for the blue values and 236 bins for the red values. I would like a colorbar where half of the bins take only 10% of the values, and the other half of the bins take the other 90% of the values.

It is not the colormap that I want non-linear, but the CLim, such that:
* all values between -4.00 and -3.97 ===> bin 1
* all values between -3.97 and -3.94 ===> bin 2
[...]
* all values between -0.03 and 0.00 ===> bin 128
* all values between 0.00 and 0.36 ===> bin 129
[...]
* all values between 45.28 and 45.64 ===> bin 255
* all values between 45.64 and 46.00 ===> bin 256
Note the difference sizes of the bins!

Subject: nonlinear colorbar

From: ImageAnalyst

Date: 18 May, 2011 18:57:51

Message: 10 of 11

This makes little sense to me - why you're transforming your image
rather than the colormap. However you can do what you said with
ordinary math. Just create two lines using point-slope formula
slope1 = (128-1)/(0 - -4);
y1 - 1 = slope1 * (x - -4)
or
y1 = slope1*(x+4)+1
then for the second sloping line

slope2 = (256 - 129)/(46 - 0)
y2 - 129 = slope2 * (x -0)
or
y2 = slope2 * x + 129

Then just use logical indexing to find the pixels in the two ranges
and apply the right formula to the right range. Untested code:

outputImage = zeros(size(imageArray));

negativePixels = imageArray < 0;
outputImage(negativePixels) = slope1 * (outputImage(negativePixels)
+4) + 1;

% and similar for positive pixels
positivePixels = imageArray >= 0;
outputImage(positivePixels ) = slope2 * outputImage(positivePixels) +
129;

Subject: nonlinear colorbar

From: pietro

Date: 29 Oct, 2011 09:53:13

Message: 11 of 11

"Carlos Adrian Vargas Aguilera" <nubeobscura@hotmail.com> wrote in message <gcdpcu$3c$1@fred.mathworks.com>...
> Or this one is better:
>
> >> figure
> >> x = [0 0.001 0.005 0.01 0.05 1 2 3 4 5];
> >> Nx = length(x);
> >> clim = [min(x) max(x)];
> >> dx = min(diff(x));
> >> y = clim(1):dx:clim(2);
> >> for k=1:Nx-1, y(y>x(k) & y<=x(k+1)) = x(k+1); end % NEW
> >> cmap = colormap(jet(Nx));
> >> cmap2 = [...
> >> interp1(x(:),cmap(:,1),y(:)) ...
> >> interp1(x(:),cmap(:,2),y(:)) ...
> >> interp1(x(:),cmap(:,3),y(:)) ...
> >> ];
> >> S = peaks(30);
> >> surf(S - min(S(:)))
> >> colormap(cmap2)
> >> caxis(clim)
> >> colorbar
>
> Carlos
>
> Anyway, this is part of the utilities I sooner or later submit to the FileExchange


Hi Carlos,

this method works, but unfortunately for a log scale dx is really small and in the color bar there is just one color. It seems the colorbar is filled just with the last n elements of colormap array. Do you have any suggestion to fix it?

Thanks

Pietro

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com