MATLAB子图绘制整个区域

时间:2014-09-05 14:56:46

标签: matlab subplot

我正在尝试使用子图进行一系列5个图(因此彼此相邻的五个图)。但是,当我运行代码时,第一个绘图将接管整个图形区域。如何运行它以使每个图都保留在每个区域?

另外,如果我希望每年都在一个不同的行,邮票样式,每行有5个图表,可以使用子图吗?现在,我每年都会运行并将每行5个图保存为单独的jpg文件。

years = 1997:2014;
for y = 1:numel(years)
    subplot(1,5,1)

    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'});
    geoshow(ax, states,'FaceColor', 'none')
    framem off; gridm off; mlabel off; plabel off

    % Plot data - For each site
    for i = 1:length(uID)
        scatterm(ax, str2double(Lat{i}), str2double(Lon{i}), 40, annual_avg_PM25(i), 'filled'); 
    end

    subplot(1,5,2) 
    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'); % Etc. Same as above

    % Plot data - For each site
    for i = 1:length(uID)
        scatterm(ax, str2double(Lat_Win{i}), str2double(Lon_Win{i}), 40, PM25_Win(i), 'filled');
    end
    % Title
    title(['PM2.5 24-hr Winter (DJF) Seasonal Average ', num2str(years(y)-1), '-', num2str(years(y))]); % Title changes every loop - Year;

    % Etc. Same format for plotting 2 more graphs
    % Save as jpg
    clf
end
close(gcf)

我得到了这个: enter image description here

修改

我尝试了下面的代码,它确实在各自的象限中绘制了子图,但是由于颜色条而弄乱了最后一个图的大小,并且标题不限于象限。我可以调整标题的大小,但是有更优雅的解决方案吗?此外,没有有效使用空白区域。

    years = 1997:2014;
    for y = 1:numel(years)

        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

        %% Annual average PM2.5 concentration
        load(['PM25_24hr_AnnualAvg_' num2str(years(y)) '.mat'], 'annual_avg_PM25', 'Date', 'Lat', 'Lon', 'uID')

        subplot(1,5,1);
        MapLatLimit = [20 50];
        MapLonLimit = [-135.5 -44];
        usamaps = shaperead('usastatelo', 'UseGeoCoords', true, ...
            'BoundingBox', [MapLonLimit' MapLatLimit']);
        ax = axesm('MapProjection', 'eqaconic', 'MapParallels', [],...
            'MapLatLimit', MapLatLimit, 'MapLonLimit', MapLonLimit,...
            'GLineStyle', '-');
        geoshow(usamaps, 'DisplayType', 'polygon', 'FaceColor','none')
        framem off; gridm off; mlabel off; plabel off

        % Title
        title(['Annual Average ', num2str(years(y))]); % Title changes every loop - Year;

        % Plot data - For each site
        for i = 1:length(uID)
            scatterm(ax, str2double(Lat{i}), str2double(Lon{i}), 40, annual_avg_PM25(i), 'filled'); 
        end

        clear('uID', 'annual_avg_PM25', 'Lat', 'Lon')

        %% Plot all the other ones in the same fashion except for the last plot, which adds a colorbar
                %% Fall Seasonal Average
        load(['PM25_24hr_FallAvg_' num2str(years(y)) '.mat'], 'annual_avg_PM25', 'Date', 'Lat', 'Lon', 'uID')

        subplot(1,5,5);
        MapLatLimit = [20 50];
        MapLonLimit = [-135.5 -44];
        usamaps = shaperead('usastatelo', 'UseGeoCoords', true, ...
            'BoundingBox', [MapLonLimit' MapLatLimit']);
        ax = axesm('MapProjection', 'eqaconic', 'MapParallels', [],...
            'MapLatLimit', MapLatLimit, 'MapLonLimit', MapLonLimit,...
            'GLineStyle', '-');
        geoshow(usamaps, 'DisplayType', 'polygon', 'FaceColor','none')
        framem off; gridm off; mlabel off; plabel off

        % Plot data - For each site
        for i = 1:length(uID)
            scatterm(ax, str2double(str2double(Lat{i})), str2double(str2double(Lon{i})), 40, annual_avg_PM25(i), 'filled'); % Plot a dot at each Lat and Lon
        end

        % Colorbar
        caxis([5 12])
        h = colorbar; %('location', 'OutsideEast');
        ylabel(h,'Concentration (ug/m3)');

        % Title
        title(['Fall (SON) Average ', num2str(years(y))]); % Title changes every loop - Year;

        % Save as jpg
        eval(['print -djpeg map_US_' num2str(years(y)) '_Subplot_AnnualSeasonalAvg_PM25_24hr.jpg']);
        clf
    end
end

这是我得到的图像: enter image description here

1 个答案:

答案 0 :(得分:1)

正如trogdor所提到的,首先调用subplot是很奇怪的。除此之外,使用figure()代替实际的数字(例如figure(1))将使每次打开一个新的数字。

您可以将ax = figure();移到for循环之外。然后调用循环中所需的子图。我也不明白你为什么要在第一个set命令中显示off。

最后回答你的问题:我相信,scatterm(ax,...)使用的是轴,而不是数字手柄。调用子图后,您应该能够gca获取当前轴句柄,或使用子图句柄:hsp = subplot(..)


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

for lp = 1:10
   subplot(2,5,lp);
   hma(lp)=axesm('MapProjection','robinson',...
    'Frame','off','Grid','on');
   usamap('conus');
   states = shaperead('usastatelo', 'UseGeoCoords', true,...
        'Selector',...
        {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});
    geoshow(hma(lp), states,'FaceColor', 'none')
    framem off; gridm off; mlabel off; plabel off
    scatterm(hma(lp), [1 2], [1 2], 40, [1 2], 'filled');
end

P.S。您不想关闭当前数字,请删除close(gcf)


重新。第二个问题:使用subplot(2,5,2)获取2行,5列子图。编号首先是列,第二行是行。即 subplot(2,5,1:5)是您的第一行,subplot(2,5,6:10)是您的第二行。