MATLAB子图不会停留在页面的一侧

时间:2014-05-22 19:37:06

标签: matlab plot figure subplot

我试图使用subplot并排绘制两张地图。然而,我只是得到一个覆盖整个页面的地图(我试图绘制的第二个地图),而不是让地块彼此相邻。像这样:

enter image description here

我希望顶部的图形是一个地图,它看起来与上面的图像相同,但有不同的颜色条,标题等。然后是底部图形。此图像也会切断颜色条,轴不应该这样标记。这是我希望它看起来像我之前在美国较小区域制作的一个例子。 enter image description here

我希望它们实际上是并排的,所以我编辑了代码来做到这一点(我正在尝试上下来看它是否会解决问题)

这是我一直在使用的代码。我可以在此处找到我加载的数据:https://www.dropbox.com/sh/k55y0g2kv9w7gv8/AADBBE9Qc1M-C7YiRL3SnC4ja

%% Load data needed for mapping
load map_PM25.mat
load map_O3.mat

nFrames = 6240; 
for k = 94:nFrames 
    subplot(1,2,1) % PM2.5

    % Map of conterminous US
    ax = figure(1);
    set(ax, 'visible', 'off', 'units','normalized','outerposition',[0 0 1 1]); %  Make window that shows up full sized, which makes saved figure clearer

    ax = usamap('conus');
    states = shaperead('usastatelo', 'UseGeoCoords', true,...
        'Selector',...
        {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});
    faceColors = makesymbolspec('Polygon',...
        {'INDEX', [1 numel(states)], 'FaceColor', 'none'}); % NOTE - colors are random
    geoshow(ax, states, 'DisplayType', 'polygon', ...
        'SymbolSpec', faceColors)
    framem off; gridm off; mlabel off; plabel off

    hold on

    % Plot data
    scatterm(ax,str2double(Lat_PM25{k})', str2double(Lon_PM25{k})', 40, str2double(data_PM25{k})', 'filled'); 

    hold on

    % Colorbar
    caxis([5 30]);
    h = colorbar;
    ylabel(h,'ug/m3');

    % Title
    title(['PM2.5 24-hr Concentration ', datestr(cell2mat(date_PM25(k)), 'mmm dd yyyy')]); 

    subplot(1,2,2) % O3
    % Map of conterminous US
    ax = figure(1);
    set(ax, 'visible', 'on', 'units','normalized','outerposition',[0 0 1 1]); %  Make window that shows up full sized, which makes saved figure clearer

    ax = usamap('conus');
    states = shaperead('usastatelo', 'UseGeoCoords', true,...
        'Selector',...
        {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});
    faceColors = makesymbolspec('Polygon',...
        {'INDEX', [1 numel(states)], 'FaceColor', 'none'}); % NOTE - colors are random
    geoshow(ax, states, 'DisplayType', 'polygon', ...
        'SymbolSpec', faceColors)
    framem off; gridm off; mlabel off; plabel off

    hold on

    % Plot data 
    scatterm(ax,str2double(Lat_O3{k})', str2double(Lon_O3{k})', 40, str2double(data_O3{k})'*1000, 'filled'); % Plot a dot at each Lat and Lon

    hold on

    % Colorbar 
    caxis([10 90]);
    h = colorbar;
    ylabel(h,'ppb');

    % Title
    title(['O3 MDA8 Concentration ', datestr(cell2mat(date_O3(k)), 'mmm dd yyyy')]); % Title changes every daytitle(str);

    % Capture the frame
    mov(k) = getframe(gcf); % Makes figure window pop up

    % Save as jpg 
    eval(['print -djpeg map_US_' datestr(cell2mat(date_PM25(k)),'yyyy_mm_dd') '_PM25_24hr_O3_MDA8.jpg']);

    clf

end

close(gcf)

2 个答案:

答案 0 :(得分:2)

问题是usamap不允许您在当前轴(在这种情况下,您的子绘图轴)中绘图。请参阅MATLAB论坛上的this discussion

基本的解决方法是构建usamap,获取此贴图的轴,并将其放入子图轴中。位置。以下代码与您希望的是并排的usamap:

h11 = subplot(1,2,1);
ax = usamap('conus');
set(ax,'Position',get(h11,'Position'));
delete(h11);

h22 = subplot(1,2,2);
ax2 = usamap('conus');
set(ax2,'Position',get(h22,'Position'));
delete(h22);

答案 1 :(得分:0)

虽然我不确定,但我认为同时使用subplot(1,2,1)set(ax, 'visible', 'off', 'units','normalized','outerposition',[0 0 1 1]);会有问题。我认为第二个陈述适用于整个数字而不是特定的子情节,这可能就是为什么它使第二个数字与全尺寸[0 0 1 1]的第一个重叠。

这是我做这样的事情的代码。

a=1:5;
b=1:5;

figure('units','normalized','outerposition',[0 0 1 1]); % this makes figure full size
subplot('position',[0.05 0.05 0.45 0.9]); % specifying the position and dimensions of each subplot
% notice i have left some space between the subplots 
plot(a,b);
subplot('position',[0.55 0.05 0.45 0.9]); % 2nd subplot
plot(a,b);

你也可以写一个循环,

posx=0.05; % x position of first subplot
for i=1:3
    subplot('position',[posx 0.05 0.25 0.9]);
    posx=posx+0.3; % this is little bit greater than x dimension 0.25 of subplot 
    % to leave space between subplots
    plot(a,b);
end