【问题标题】:MATLAB Subplots plotting over whole area绘制整个区域的 MATLAB 子图
【发布时间】:2014-09-05 14:56:45
【问题描述】:

我正在尝试使用子图制作一排 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)

我得到了这个:

编辑

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

    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

这是我得到的图像:

【问题讨论】:

  • 我无法确定运行您的代码(未定义的变量 'uID'),但我很确定问题在于您运行这些命令的顺序。子图调用可能需要在ax = figure(); 命令之后。
  • 我在 ax = figure(); 下试过了它什么也没做。
  • 使用带有句柄的“get”和“set”命令来调整每个子图的大小和位置。键入“get(hsp(1))”以查看可以更改的属性。相当多的工作虽然..祝你好运。

标签: matlab subplot


【解决方案1】:

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

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

最后回答你的问题:我相信, scatterm(ax,...) 使用轴,而不是图形句柄。调用 subplot 后,您应该可以 gca 获取当前轴句柄,或使用 subplot 句柄: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

附言不想关闭当前图,删除close(gcf)


回复。你的第二个问题:使用subplot(2,5,2) 得到一个 2 行 5 列的子图。编号是列第一,行第二。 IE。 subplot(2,5,1:5) 是你的第一行,subplot(2,5,6:10) 你的第二行。

【讨论】:

  • 我将ax = figure(1) 放在循环之外并且只放在那里。我在有subplot(...) 的任何地方都使用了hsp = subplot(...)。但我只是没有得到任何显示。
  • 抱歉回复晚了。您的代码可以正常工作,但是当我尝试上面的操作时,将scatterm(hsp(lp), ... 放在framem off 之后,我收到此错误:Error using gcm (line 26) Not a map axes. Error in scatterm (line 49) gcm(ax);
  • 当我运行添加了 scatterm(hsp(lp)...) 的代码时,它给出了我上面提到的错误。所以那部分有问题。我已对我的问题添加了一个编辑以反映我正在尝试的内容,并添加了指向我使用的 .mat 文件的链接,以便您自己进行测试。
  • 嘿,抱歉,但我意识到虽然坐标轴按我想要的那样绘制(一行 5 个),但美国本身的地图并没有显示出来。你知道为什么吗?此外,代码似乎无法绘制超过一年的时间。在前 5 个轴出现后,它只是卡在空白处。我不知道这是否与外面有figureset(ax...) 有什么关系。
  • 上述编辑最终在整个图像上再次绘制了美国地图,左侧有一个小轴 XY 图。
猜你喜欢
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多