Set the Subplot Apsect Ratio Manually
조회 수: 74 (최근 30일)
이전 댓글 표시
hi, I have a problem with setting the AR of one of my subplots. lets try to make this more understandable. first of all, here is a part of the code
scrsz = get(0,'ScreenSize');
figure('Position',[1 scrsz(2) scrsz(3) scrsz(4)]);
s1=subplot(2,6,1:5); hold on; grid on; box on; axis([totminx 1.02 totminz totmaxz]);
dx=1.02-totminx;
dz=totmaxz-totminz;
h=0.3412;
w=h*dx/dz;
set(s1,'FontSize',15, 'Position', [0.13 ,0.5838 ,w ,h])
plot(poly(:,1), poly(:,2), 'k', 'LineWidth', 2)
plot(x1, z1, 's', 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r', 'MarkerSize', 5);
ok, what i want to get is original proportions for the plot, meaning that the dist between 0-0.1 will look the same in the x and y axis. (does not mean a square image!)
also, i want to do this manually by changing the position of the subplot itself.
i want to do this because when i use the simple way of: set(s1, 'DataAspectRatio', [ 1 1 1]) other annotations that i add afterwards are not connected and seem to be floating, this is because the "DAR" doesn't change the subplot's position.
hope that you can at least understand my question now :( sorry about the confusion caused.
thanks Noa
댓글 수: 5
Daniel Shub
2012년 9월 19일
At this point I am going to flag this question. Noa, please edit the question to provide some code and ideally an image that demonstrates the problem and a clear and concise explanation of what you see and what you expect to see.
채택된 답변
José-Luis
2012년 9월 19일
편집: José-Luis
2012년 9월 19일
That would depend on the aspect ratio of your figure box, for instance:
h = figure;
pos = get(h,'Position');
ratio = pos(3) / pos(4);
a = plot(rand(10,1));
aH = ancestor(a,'axes');
desiredWidth = 0.6; %for example
desiredHeight = desiredWidth * ratio;
posA = get(aH,'Position');
posA(3) = desiredWidth;
posA(4) = desiredHeight;
set(aH,'Position',posA);
Will give you a square plot. You would need to recalculate every time you resize your figure. Also, the desired width has to be selected such that you do not overstep the boundaries of your figure box. It should work for a subplot as well, you just have to be careful with desiredWidth.
ADD Say you know in advance your xlim and ylim (or you get them afterwards), maybe this is what you are looking for:
xlim = [0 5];
ylim = [0 10];
ratio = diff(xlim)/diff(ylim);
h = plot( 1:10);
aH = ancestor(h,'axes');
axis equal;
set(aH,'PlotBoxAspectRatio',[1 ratio 1])
set(aH,'XLim',xlim,'YLim',ylim);
댓글 수: 8
José-Luis
2012년 9월 19일
편집: José-Luis
2012년 9월 19일
Annotations are defined with respect to the figure box, not to the axes limits. It should be posssible to rescale, but it would be rather convoluted. In your case, it would involve translation and shear, you could get the transformation matrix using the position (->translation) and scale (-> shear) of your original and final axes (in "figure" units), then multiply the starting and ending coordinates of your arrows by the transformation matrix and then redraw). As i said, convoluted.
It looks like you are ploting tangent vectors. Maybe, it would be better to use the quiver built in function. That would save some headaches.
Or maybe I am still missing the point and there is a simpler answer.
추가 답변 (2개)
Daniel Shub
2012년 9월 19일
I think what you are looking for is:
set(gca, 'PlotBoxAspectRatio', [1,1,1])
Daniel Shub
2012년 9월 19일
편집: Daniel Shub
2012년 9월 19일
I think the problem is that the default unit of an axis object is normalized and the default unit of a figure object is pixels. This means that if the figure is not square than an axis with a position of [x, y, a, a] will not be square. If you want an axis with a position of [x, y, a, a] to be square, or more accurately if you want [x, y, a, b] to have an aspect ratio of a/b, then you need a square figure. Assuming that get(0, 'MonitorPositions') returns something meaningful (see: dual monitor support), then you should be able to do
pos = get(0, 'MonitorPositions');
set(gcf, 'Position', [1, 1, 0.5*min(pos(1, 3:4)), 0.5*min(pos(1, 3:4))]);
set(gca, 'Position', [0.1, 0.1, 0.8, 0.8]);
While I agree that afterwards a set(gca, 'PlotBoxAspectRatio', [1,1,1]), should have no effect, on my system it does. There is a slight change in the tight inset, but I don't think this affects the size of the axis directly.
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!