【发布时间】: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))”以查看可以更改的属性。相当多的工作虽然..祝你好运。