Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
contourf() strange with only few levels

Subject: contourf() strange with only few levels

From: Armin Mueller

Date: 10 Oct, 2011 19:40:01

Message: 1 of 4

Dear NG,

I would like to plot measurement data as contour map. However, no matter
how many contour levels you choose, the default colorbar always has 64
shadings (the default size of a color map for a figure).

Here is an example how I try to come around this. The colormap is
handmade, and the limits are set accordingly. It works for some cases,
but *not* for 3, 6, 9, 11, 12, 15, ... discrete levels.

Does anybody have a clue what I did wrong?

Thank you for any hint!
Armin


% there are double contour levels showing up for lev = 3 6 9 11 12 15 ...

Z_max = 100; % end of scale
Z = Z_max*rand(7, 7); % random data

for lev=2:8

     figure(lev)

     [cs, h] = contourf(Z, (0:lev)/lev*Z_max);
     clabel(cs, h)
     caxis([0 Z_max])
     colormap(jet(lev))

     xlabel('X')
     ylabel('Y')

     cb = colorbar('Location', 'SouthOutside');
     xlabel(cb, 'Z')
     set(cb, 'xtick', (0:lev)/lev*Z_max)

end

Subject: contourf() strange with only few levels

From: Yuheng

Date: 11 Oct, 2011 06:29:11

Message: 2 of 4

Armin Mueller <arm.in@web.de> wrote in message <j6vhmh$3kj$1@news2.rz.uni-karlsruhe.de>...
> Dear NG,
>
> I would like to plot measurement data as contour map. However, no matter
> how many contour levels you choose, the default colorbar always has 64
> shadings (the default size of a color map for a figure).
>
> Here is an example how I try to come around this. The colormap is
> handmade, and the limits are set accordingly. It works for some cases,
> but *not* for 3, 6, 9, 11, 12, 15, ... discrete levels.
>
> Does anybody have a clue what I did wrong?
>
> Thank you for any hint!
> Armin
>
>
> % there are double contour levels showing up for lev = 3 6 9 11 12 15 ...
>
> Z_max = 100; % end of scale
> Z = Z_max*rand(7, 7); % random data
>
> for lev=2:8
>
> figure(lev)
>
> [cs, h] = contourf(Z, (0:lev)/lev*Z_max);
> clabel(cs, h)
> caxis([0 Z_max])
> colormap(jet(lev))
>
> xlabel('X')
> ylabel('Y')
>
> cb = colorbar('Location', 'SouthOutside');
> xlabel(cb, 'Z')
> set(cb, 'xtick', (0:lev)/lev*Z_max)
>
> end


That's interesting.
I think it's a problem with calculation error of floating-point number. There maybe small errors between (0:lev)/lev*Z_max and jet(lev)

use
[cs, h] = contourf(Z, double(vpa((0:lev)/lev*Z_max)));
will fix it

Subject: contourf() strange with only few levels

From: Armin Mueller

Date: 11 Oct, 2011 09:30:08

Message: 3 of 4

Yuheng wrote:

> That's interesting.
> I think it's a problem with calculation error of floating-point number.
> There maybe small errors between (0:lev)/lev*Z_max and jet(lev)
>
> use
> [cs, h] = contourf(Z, double(vpa((0:lev)/lev*Z_max)));
> will fix it

Wow, it is getting even more interesting.

With your fix, the cases 3 and 11 are working well now. However, I still
have trouble with Z_max=100 and number of levels 6, 9, 12, 15. The case
6 would be most interesting because to my understanding it seems to be
ideal for B/W printing.

When I change Z_max to 120, the cases 6, 12 and 15 are working, 9 is
not. => I guess that contourf() might have a general problem with those
contour levels that lie short below infinite fractions.

What if we make your fix the other way round, not calculate more
precise, but round up? => Dirty hack, but looks good.

[cs, h] = contourf(Z, ceil((0:lev)/lev*Z_max*1000)/1000);

Best regards,
Armin

Subject: contourf() strange with only few levels

From: Michael Völker

Date: 4 Sep, 2012 15:58:08

Message: 4 of 4

Armin Mueller <arm.in@web.de> wrote in message <j6vhmh$3kj$1@news2.rz.uni-karlsruhe.de>...
> Dear NG,
>
> I would like to plot measurement data as contour map. However, no matter
> how many contour levels you choose, the default colorbar always has 64
> shadings (the default size of a color map for a figure).

Armin,

I created a wrapper for the colormap function, intended to use more shadings:
http://www.mathworks.com/matlabcentral/fileexchange/37971

It would be interesting to know if this solves your problem.

Michael

Tags for this Thread

No tags are associated with this thread.

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.

Contact us