2014-09-05 139 views
0

我想用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

回答

1

由於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