【问题标题】:matlab plot with multiple colormaps具有多个颜色图的matlab绘图
【发布时间】:2017-07-19 18:06:43
【问题描述】:

我想创建一个带有 pcolor 图的图,顶部有一个等高线图。两者都有不同的颜色图 - pcolor 带有“hot”,contour 带有“gray”。

我较新的 Matlab 版本可以使用多个颜色图。

代码有效,但两个轴不重叠,即使轴位置同步。

%% 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

【问题讨论】:

    标签: matlab colormap


    【解决方案1】:

    如果您不使用ax2.Visible = 'off',您可能会看到坐标轴的位置不同,因为第一个坐标轴被压扁以便为第二坐标轴没有的颜色条留出空间。

    TL;DR

    需要将位置属性设置为相等

    ax2.Position = ax1.Position
    

    演示

    你可以用一个空白图来模拟这个:

    1.

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

    输出:

    2.

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

    输出(注意第二个轴填充了第一个 + 颜色条的空间):

    3.

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

    输出(注意较粗的数字显示它们完全重叠):

    【讨论】:

    • 我的问题是由 axis equal 引起的,它只在第一个情节中被调用。一旦我为第二个情节调用它并同步位置一切正常。
    • 很高兴它可以解决,我之前没有看到使用过多个颜色图,所以这是一个有趣的查询。
    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2018-09-29
    • 2015-01-09
    • 1970-01-01
    • 2013-10-05
    • 2020-09-26
    相关资源
    最近更新 更多