Clear Filters
Clear Filters

xlim and ylim auto not working

33 views (last 30 days)
Hello
I am plotting some data and some fits to the data in a 2x2 subplot. The automatic x and y limits to the axis does not seem to be working. (or maybe they are scaling to 'nice' numbers?) Also finding the max and min values for each plot is not easy. I could use the axis handles ax.YData ect, however I am using the plot command multiple times to control the colors, which means not all lines are containted in the original axis handle.
Does anyone know of an easy way to set the x and y axis limits to the max and min values of all the data in a plot?
%% Load the data
FileP = 'filepath';
FileN = 'filename.mat';
load(strcat(FileP,FileN));
colors ={'#0072BD' '#D95319' '#EDB120' '#7E2F8E' '#77AC30' '#4DBEEE' '#A2142F'}; %default plot colors
%% Plot the data and initial guess
%r = rates [k1, k2]
r0{1} = [0.0145556113352403;0.00859211428374791];
r0{2} = [0.00951438529846522;0.00769415339659029];
r0{3} = [0.00464048796975012;0.00576406332592959];
r0{4} = [0.00424324725408617;0.00441517808025549]
figure(FigC);
for i=1:N_temps
subplot(3,2,i);
ax{i} = plot(data{i}(:,1),data{i}(:,[2:4]),'-o','markersize',4);
title(sheetnames(i));
tspan = [data{i}(1,1) data{i}(end,1)];
y0 = data{i}(1,[2:4]);
sol = ode15s(@(t,y)diffun_NonEq(t,y,r0{i}),tspan,y0(1,:));
hold on;
plot(sol.x,sol.y(1,:),'-o','markersize',4,'Color',colors{1});
plot(sol.x,sol.y(2,:),'-o','markersize',4,'Color',colors{2});
plot(sol.x,sol.y(3,:),'-o','markersize',4,'Color',colors{3});
xlim auto;
hold off;
end
sax = subplot(3,2,[5:6]);
posspl = get(sax,'position'); % Getting its position
lgd = legend(sax,ax{1},Header{1,[2:4]});
set(lgd,'position',posspl); % Adjusting legend's position
axis(sax,'off'); % Turning its axis off
lgd.NumColumns = N_states;
FigC = FigC + 1;

Accepted Answer

Adam Danz
Adam Danz on 1 May 2020
Edited: Adam Danz on 1 May 2020
I think what you're looking for is,
h = subplot(3,2,i);
axis(h,'tight')
or just
axis tight
applied to the current axes.
  4 Comments
Adam Danz
Adam Danz on 1 May 2020
Yes, instead of setting all 4 elements of the legend position, just set the first 2 elements (the position of the left, bottom corner) and don't change elements 3 & 4 (width and height).
george hargenrader
george hargenrader on 1 May 2020
Thanks! I ended up playing around with the position a bit, but that helped
For the curious:
sax = subplot(2,2,1);
lgd = legend(sax,ax{1},Header{1,[2:4]});
set(lgd,'position',[0.5 0.04 0 0]) % Adjusting legend's position
lgd.NumColumns = N_states;
lgd.FontSize = 10;
lgd.Box = 'off';
sax = subplot(2,2,3);
possax = get(sax,'position');
possax(2) = possax(2) +0.03;
set(sax,'position',possax) % Adjusting plot 3's position
sax = subplot(2,2,4);
possax = get(sax,'position');
possax(2) = possax(2) +0.03;
set(sax,'position',possax) % Adjusting plot 4's position
FigC = FigC + 1;

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!