MATLAB绘制整个区域的小区图

问题描述:

我想用5个小区做一排5个小区(所以5个小区彼此相邻)。但是,当我运行代码时,第一个图只会接管整个图形区域。我如何运行它,以便每个区域都留在每个区域?MATLAB绘制整个区域的小区图

另外,如果我想每年都在不同的行中,邮票风格,每行有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

+0

我无法确定运行你的代码(未定义的变量'uID'),但我很确定问题在于你运行这些命令的顺序。子图调用可能需要在'ax = figure();'命令之后。 – Trogdor 2014-09-05 15:30:36

+0

我尝试了下ax = figure();并没有做任何事情。 – shizishan 2014-09-05 16:06:46

+0

使用带有手柄的'get'和'set'命令来调整每个子图的大小和位置。输入'get(hsp(1))'来查看你可以改变的属性。相当一些工作虽然..祝你好运。 – 2014-09-09 18:48:04

由于Trogdor的提到了,奇怪的是先调用的插曲。除此之外,使用figure()而不是实际的数字(例如figure(1))将使它每次都开启新的数字。

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

为了最终回答你的问题:我相信,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 

附:您不想关闭当前数字,请删除close(gcf)


Re。你的第二个问题:使用subplot(2,5,2)获得2行5列的子图。编号是列第一,第二行。即 subplot(2,5,1:5)是您的第一排,subplot(2,5,6:10)您的第二排。

+0

我把'ax = figure(1)'放在循环外面,只在那里。我在任何有'subplot(...)'的地方使用过'hsp = subplot(...)'。但我只是没有得到任何东西出现。 – shizishan 2014-09-05 16:35:10

+0

对不起,延迟回复。你的代码是可行的,但是当我尝试了上面的内容时,在'framem off'后面加上'scatterm(hsp(lp),...'),我得到这个错误:'Error using gcm(line 26) 不是地图坐标轴。 散布(第49行)的错误 gcm(ax);' – shizishan 2014-09-07 20:08:07

+0

当我用scatterm(hsp(lp)...)运行你的代码时,它给出了我上面说过的错误。所以这部分有问题。我在我的问题中添加了一个编辑,以反映我正在尝试的内容,并将其放入到我使用的.mat文件的链接中,以便您自己测试它。 – shizishan 2014-09-07 20:31:14