5
votes

I am using pcolor together with contour lines. However, the value of the lines can not be identified from the plot, as can be seen in the following picture.

[x y data] = peaks(1000);
data = data / max(max(data));

colorDepth = 1000;
colormap(jet(colorDepth));

hold on;
pcolor(x,y,data); shading flat

[C,hfigc] = contour(x, y, data,[0:0.1:1]);
set(hfigc, ...
    'LineWidth',1.0, ...
    'Color', [1 1 1]);
hold off;
hcb = colorbar('location','EastOutside');

enter image description here

I would rather want the pcolor to be in gray values and the contour lines in colors. However then I need a legend for the contour lines as well.

EDIT: It works somehow by combining two colormaps, but then the colorbar shows both, which is not what I want. I would rather want to have a colorbar which includes the same contour lines as the plot.

[x y data] = peaks(1000);
data = data - min(min(data));
data = data / max(max(data));

colorDepth = 1000;

hold on;
caxis([-1 1]);
colormap([gray(colorDepth); jet(colorDepth)]);
hplot = pcolor(x,y,data); shading flat        

[C,hfigc] = contour(x, y, data-1,[-1:0.1:0]);
set(hfigc, 'LineWidth',1.0);
% set(hfigc, 'Color', [1 1 1]);

hold off;
hcb = colorbar('location','EastOutside');

EDIT: The colorbar can be corrected with

set(hcb, 'Ylim', [0 1]);

enter image description here

1
Your code lacks definition for zlevs (and have a small typo) - Itamar Katz
I fixed the typo. But any smallest hint on how to color the contour different than the plot would be helpful - Matthias Pospiech
@MatthiasPospiech: colorbar is actually an axes. You can change any of its properties, like x/y limits, color limits, position etc. You can also simulate colorbar or add additional colorbar creating it as a separate pcolor/imagesc plot. - yuk
@yuk: how can I get the axes of the colorbar, or in general how can I get the axis handle of any plot? - Matthias Pospiech
You already have it. It's hcb. So you can do for example:set(hcb,'ylim',[0 1]). The handle for current axes is gca. In general, look at doc of graphic function you are using, it usually can return the axes handle. - yuk

1 Answers

5
votes

Besides the solution presented already in the question it is possible to use the tools freezeColors and COLORMAP and COLORBAR utilities to change the colormap in a single figure

addpath('cm_and_cb_utilities');
addpath('freezeColors');

figure(1); clf;
[x y data] = peaks(1000);
data = data - min(min(data));
data = data / max(max(data));

colorDepth = 1000;

hold on;
caxis([0 1]);
colormap(jet(colorDepth));
hplot = pcolor(x,y,data); shading flat        

hcb = colorbar('location','EastOutside');
set(hcb, 'Ylim', [0 1]);
cbfreeze;

freezeColors;

colormap(gray(colorDepth));
[C,hfigc] = contour(x, y, data,[0:0.1:1]);
set(hfigc, 'LineWidth',1.0);

hold off;

enter image description here