Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: colorbar problem

Subject: colorbar problem

From: carmelo

Date: 06 Mar, 2008 15:09:02

Message: 1 of 6

hello,

i have 2 matrices A and B. A contains n unique values and
B contains m<n values (a subset of n)

i want to plot both matrices using the same colors and a
common colorbar
to keep things easy i created another matrix C=[A B]
and i do:

figure
imagesc(C);
n=length(unique(C));
colormap(jet(n))
colorbar('YTick',1:n,'YTickLabel',unique(C))

but it does not work... i see that the same color is used
for two different values and the colorbar has all the
elements grouped down and not nicely distributed along the
bar.

any suggestions on how to improve this "poor" script?

Thanks in advance!!

Subject: Re: colorbar problem

From: us

Date: 06 Mar, 2008 15:51:02

Message: 2 of 6

"carmelo ":
<SNIP colorbar-tick headache...

one of the solutions

% the data
     a=magic(5);
     b=fix(a/2)-1;
     c=[a,b];
% the engine
     cu=unique(c);
     n=numel(cu);
     imagesc(c);
     colormap(jet(n));
     ch=colorbar;
     yl=get(ch,'ylim');
     ys=diff(yl)/n;
     yt=linspace(yl(1)+ys,yl(2),n)-.5*ys;
% - the colorbar
     set(ch,'ytick',yt,'yticklabel',cu);

us

Subject: Re: colorbar problem

From: carmelo

Date: 06 Mar, 2008 17:03:02

Message: 3 of 6

"us " <us@neurol.unizh.ch> wrote in message <fqp3t6
$aae$1@fred.mathworks.com>...
> "carmelo ":
> <SNIP colorbar-tick headache...
>
> one of the solutions
>
> % the data
> a=magic(5);
> b=fix(a/2)-1;
> c=[a,b];
> % the engine
> cu=unique(c);
> n=numel(cu);
> imagesc(c);
> colormap(jet(n));
> ch=colorbar;
> yl=get(ch,'ylim');
> ys=diff(yl)/n;
> yt=linspace(yl(1)+ys,yl(2),n)-.5*ys;
> % - the colorbar
> set(ch,'ytick',yt,'yticklabel',cu);
>
> us

Thanks us!!

Subject: Re: colorbar problem

From: carmelo

Date: 07 Mar, 2008 09:24:02

Message: 4 of 6

"carmelo " <km.nospam.scriba.nospam@yahoo.esnospam> wrote
in message <fqp846$a3c$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <fqp3t6
> $aae$1@fred.mathworks.com>...
> > "carmelo ":
> > <SNIP colorbar-tick headache...
> >
> > one of the solutions
> >
> > % the data
> > a=magic(5);
> > b=fix(a/2)-1;
> > c=[a,b];
> > % the engine
> > cu=unique(c);
> > n=numel(cu);
> > imagesc(c);
> > colormap(jet(n));
> > ch=colorbar;
> > yl=get(ch,'ylim');
> > ys=diff(yl)/n;
> > yt=linspace(yl(1)+ys,yl(2),n)-.5*ys;
> > % - the colorbar
> > set(ch,'ytick',yt,'yticklabel',cu);
> >
> > us
>
> Thanks us!!
>

oops! it seems that it does not work like i wanted it to
work. i have 6 unique elements in c but when i do:
imagesc(c);
colormap(jet(n));
the same color is assigned to two different values in c
and the colorbar is not correct (e.g. value 27 gets yellow
but in the colorbar yellow corresponds to value 35).
am i doing something wrong?

this is c:

c=[
0 0 0 0 27 27 27 0
0 0 0 0 0 0 0
0 0 0 0 27 27 27
0 0 0 0 0 0 0
0
0 0 0 0 27 27 27 27
27 35 0 0 0 0 0
0 0 0 0 27 27 27
27 27 35 0 0 0 0
0
0 0 0 27 25 25 27 27
27 27 35 27 0 0 0
0 0 0 27 25 25 27
27 27 27 35 27 0 0
0
0 0 0 25 25 25 27 27
27 27 27 27 27 25 25
0 0 0 25 25 25 27
27 27 27 27 27 27 25
25
0 0 27 25 25 25 25 27
27 27 27 27 27 27 0
0 0 27 25 25 25 25
27 27 27 27 27 27 27
0
0 0 25 25 25 25 25 25
27 27 27 27 27 27 0
0 0 25 25 25 25 25
25 27 27 27 27 27 27
0
0 0 25 27 27 25 25 25
27 27 27 27 27 33 0
0 0 25 27 27 25 25
25 27 27 27 27 27 27
0
0 25 25 25 27 25 25 25
27 27 27 27 33 0 0
0 25 25 25 27 25 25
25 27 27 27 27 27 0
0
0 27 27 25 25 25 25 27
25 27 27 38 38 0 0
0 27 27 25 25 25 25
27 25 27 27 27 27 0
0
0 25 25 25 25 25 25 27
27 27 27 38 0 0 0
0 25 25 25 25 25 25
27 27 27 27 27 0 0
0
25 25 27 27 27 27 27 27
27 27 27 27 0 0 0
25 25 27 27 27 27 27
27 27 27 27 27 0 0
0
0 0 0 27 27 27 27 27
27 27 27 27 0 0 0
0 0 0 27 27 27 27
27 27 27 27 27 0 0
0
0 0 0 0 0 27 27 27
27 27 27 0 0 0 0
0 0 0 0 0 27 27
27 27 27 27 0 0 0
0
0 0 0 0 0 0 0 0
27 27 27 0 0 0 0
0 0 0 0 0 0 0
0 27 27 27 0 0 0
0
];



Subject: Re: colorbar problem

From: us

Date: 07 Mar, 2008 10:02:04

Message: 5 of 6

"carmelo ":
<SNIP useless amount of data...

do not copy/paste a huge matrix, which - to boot - cannot
be recovered due formatting issues; this is tedious...

create the smallest possible data set that reproduces your
problem...

now, looking at some of the data it seems you have
something like

     c=[0,25:27,33,37,38];

the engine proposed earlier assumes that your <cu> is
continuous!
otherwise, simply adjust the engine as follows

     c=[
          0,25,26,33
          27,0,35,38
     ];
% the engine
     cu=unique(c);
     [cx,cx]=histc(c,cu); % <- add this!
     n=numel(cu);
     imagesc(cx); % <- use this!
     colormap(jet(n));
     ch=colorbar;
     yl=get(ch,'ylim');
     ys=diff(yl)/n;
     yt=linspace(yl(1)+ys,yl(2),n)-.5*ys;
% - the colorbar
     set(ch,'ytick',yt,'yticklabel',cu);

us

Subject: Re: colorbar problem

From: carmelo

Date: 07 Mar, 2008 10:24:02

Message: 6 of 6

"us " <us@neurol.unizh.ch> wrote in message
<fqr3qr$82u$1@fred.mathworks.com>...
> "carmelo ":
> <SNIP useless amount of data...
>
> do not copy/paste a huge matrix, which - to boot -
cannot
> be recovered due formatting issues; this is tedious...
>
> create the smallest possible data set that reproduces
your
> problem...
>
> now, looking at some of the data it seems you have
> something like
>
> c=[0,25:27,33,37,38];
>
> the engine proposed earlier assumes that your <cu> is
> continuous!
> otherwise, simply adjust the engine as follows
>
> c=[
> 0,25,26,33
> 27,0,35,38
> ];
> % the engine
> cu=unique(c);
> [cx,cx]=histc(c,cu); % <- add this!
> n=numel(cu);
> imagesc(cx); % <- use this!
> colormap(jet(n));
> ch=colorbar;
> yl=get(ch,'ylim');
> ys=diff(yl)/n;
> yt=linspace(yl(1)+ys,yl(2),n)-.5*ys;
> % - the colorbar
> set(ch,'ytick',yt,'yticklabel',cu);
>
> us

sorry about posting the original c matrix, i thought that
it could help... I regretted my decision when i saw the re-
formating after posting the message....and, indeed, the
message was that the values are not continous

Thanks, this solves the problem!

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
histc us 07 Mar, 2008 05:04:54
diff us 06 Mar, 2008 10:55:07
linspace us 06 Mar, 2008 10:55:07
imagesc us 06 Mar, 2008 10:55:07
code us 06 Mar, 2008 10:55:07
colormap carmelo 06 Mar, 2008 10:10:24
colorbar carmelo 06 Mar, 2008 10:10:24
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics