xlim and ylim auto not working
조회 수: 33 (최근 30일)
이전 댓글 표시
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289515/image.png)
%% 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;
댓글 수: 0
채택된 답변
Adam Danz
2020년 5월 1일
편집: Adam Danz
2020년 5월 1일
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
Adam Danz
2020년 5월 1일
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).
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!