简体   繁体   中英

matlab plot with multiple colormaps

I want to create a plot with pcolor plot with an contour plot on top. Both with different colormaps - pcolor with "hot", the contour with "gray".

I newer Matlab version multiple colormaps are possible.

The code works, however both axis do not overlap, even if the axes positions are in sync.

在此处输入图片说明

%% prepare Data
Data2D = peaks(100);
Data2D = Data2D -min(Data2D(:));
Data2D = Data2D/max(Data2D(:)) * 100;
steps = 0:05:100;

xAxis = 1:size(Data2D,2);
yAxis = 1:size(Data2D,1);

figure(1); clf
ax1 = axes;

hold on;
% 3D flat plot
caxis([0 100]);
cmap = fliplr(jet(1000));
colormap(ax1, cmap(1:800,:));

hplot = pcolor(ax1, xAxis, yAxis, Data2D);
shading flat; % do not interpolate pixels

set(ax1,'XLim',[xAxis(1) xAxis(end)]);
set(ax1,'YLim',[yAxis(1) yAxis(end)]);

% colorbar
hcb = colorbar('location','EastOutside');    
set(hcb, 'Ylim', [0 100]);


%% contour plot
ax2 = axes; linkaxes([ax1,ax2])

colormap(ax2, flipud(gray(1000)));
[C,hfigc] = contour(ax2, xAxis, yAxis, Data2D,steps);

% Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];

set(hfigc, 'LineWidth',1.0);        

hold off;
drawnow

If you didn't use ax2.Visible = 'off' you would probably see that the axes' positions are different, since the first axes are squashed to allow room for the colorbar which the second axes don't have.

TL;DR

You need to set the position properties to be equal

ax2.Position = ax1.Position

Demo

You can simulate this with a blank figure:

1.

% Create figure and first axes, which have a colorbar
figure(1)
ax1 = axes();
colorbar('location', 'eastoutside');

Output:

1个

2.

% Add new axes
hold on;
ax2 = axes();

Output (notice the second axes fills the space of the first + colorbar):

2

3.

% Make the same, so that the second axes also allow for the colorbar
ax2.Position = ax1.Position;

Output (notice thicker numbers showing they are overlapping fully):

3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM