SugaKookie
SugaKookie

Reputation: 790

MATLAB Subplot not staying on one side of the page

I'm trying to plot two maps side by side using subplot. However, instead of getting the plots to show up next to each other, I just get one map (the second one I was trying to plot) covering the whole page. Like this:

enter image description here

I want to have the top graph be one map, which will look the same as the image above but have a different colorbar, title, etc. And then the bottom graph. This image also cuts off the colorbar and the axes should not be labelled like that. Here's an example of what I would want it to look like based off of one I made before for a smaller area of the US. enter image description here

I would like them to be side by side actually, so I've edited the code to do that (I was trying up and down to see if it would solve the issue)

Here's the code I've been using. The data I'm loading can be found here: https://www.dropbox.com/sh/k55y0g2kv9w7gv8/AADBBE9Qc1M-C7YiRL3SnC4ja

%% Load data needed for mapping
load map_PM25.mat
load map_O3.mat

nFrames = 6240; 
for k = 94:nFrames 
    subplot(1,2,1) % PM2.5

    % Map of conterminous US
    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'});
    faceColors = makesymbolspec('Polygon',...
        {'INDEX', [1 numel(states)], 'FaceColor', 'none'}); % NOTE - colors are random
    geoshow(ax, states, 'DisplayType', 'polygon', ...
        'SymbolSpec', faceColors)
    framem off; gridm off; mlabel off; plabel off

    hold on

    % Plot data
    scatterm(ax,str2double(Lat_PM25{k})', str2double(Lon_PM25{k})', 40, str2double(data_PM25{k})', 'filled'); 

    hold on

    % Colorbar
    caxis([5 30]);
    h = colorbar;
    ylabel(h,'ug/m3');

    % Title
    title(['PM2.5 24-hr Concentration ', datestr(cell2mat(date_PM25(k)), 'mmm dd yyyy')]); 

    subplot(1,2,2) % O3
    % Map of conterminous US
    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');
    states = shaperead('usastatelo', 'UseGeoCoords', true,...
        'Selector',...
        {@(name) ~any(strcmp(name,{'Alaska','Hawaii'})), 'Name'});
    faceColors = makesymbolspec('Polygon',...
        {'INDEX', [1 numel(states)], 'FaceColor', 'none'}); % NOTE - colors are random
    geoshow(ax, states, 'DisplayType', 'polygon', ...
        'SymbolSpec', faceColors)
    framem off; gridm off; mlabel off; plabel off

    hold on

    % Plot data 
    scatterm(ax,str2double(Lat_O3{k})', str2double(Lon_O3{k})', 40, str2double(data_O3{k})'*1000, 'filled'); % Plot a dot at each Lat and Lon

    hold on

    % Colorbar 
    caxis([10 90]);
    h = colorbar;
    ylabel(h,'ppb');

    % Title
    title(['O3 MDA8 Concentration ', datestr(cell2mat(date_O3(k)), 'mmm dd yyyy')]); % Title changes every daytitle(str);

    % Capture the frame
    mov(k) = getframe(gcf); % Makes figure window pop up

    % Save as jpg 
    eval(['print -djpeg map_US_' datestr(cell2mat(date_PM25(k)),'yyyy_mm_dd') '_PM25_24hr_O3_MDA8.jpg']);

    clf

end

close(gcf)

Upvotes: 2

Views: 555

Answers (2)

shimizu
shimizu

Reputation: 998

The problem is that usamap does not allow you to plot in the current axes (in this case, your subplot axes). See this discussion on the MATLAB forums.

The basic workaround is to construct the usamap, get the axes for this map, and put it into the subplot axes' position. The following code has the usamap side by side as you would like it to be:

h11 = subplot(1,2,1);
ax = usamap('conus');
set(ax,'Position',get(h11,'Position'));
delete(h11);

h22 = subplot(1,2,2);
ax2 = usamap('conus');
set(ax2,'Position',get(h22,'Position'));
delete(h22);

Upvotes: 2

Guddu
Guddu

Reputation: 2437

Though i am not sure, i think using subplot(1,2,1) and set(ax, 'visible', 'off', 'units','normalized','outerposition',[0 0 1 1]); at the same time is problematic. The second statement i think works for the whole figure and not a particular subplot, that may be why it is making the 2nd figure overlap the first at full size [0 0 1 1].

here is my code to do such a thing.

a=1:5;
b=1:5;

figure('units','normalized','outerposition',[0 0 1 1]); % this makes figure full size
subplot('position',[0.05 0.05 0.45 0.9]); % specifying the position and dimensions of each subplot
% notice i have left some space between the subplots 
plot(a,b);
subplot('position',[0.55 0.05 0.45 0.9]); % 2nd subplot
plot(a,b);

you can also write a loop,

posx=0.05; % x position of first subplot
for i=1:3
    subplot('position',[posx 0.05 0.25 0.9]);
    posx=posx+0.3; % this is little bit greater than x dimension 0.25 of subplot 
    % to leave space between subplots
    plot(a,b);
end

Upvotes: 0

Related Questions