keep exact position of subplot axes

13 ビュー (過去 30 日間)
Petr Kolar
Petr Kolar 2025 年 1 月 28 日 14:22
コメント済み: Petr Kolar 2025 年 1 月 29 日 9:49
I want to plot three 2D projections of some 3D data. For compatibility with other images (our convention) I need to keep the position of the corresponding axes identical - see Fig.
Is there any (simple) way to do this? The layout in the image is the result of interactive manipulation with the size of the image - not entirely convenient :-(
'Subplot' or 'nexttile' with tiledlayout(2,2,'TileSpacing','none'); can't do it.
Of course, it would be possible to plot all the data in one axis (after a simple recalculation of the coordinates), but the advantage of individual axes for each (sub)projection would be lost ...
function plot3x2D
close all
clear all
NN=10;
x=rand(1,NN);
y=rand(1,NN);
z=rand(1,NN);
val=rand(1,NN);
figure; hold on
axis off
a11=axes('pos',[0.05 0.5 0.45 0.45],'XaxisLocation','top',"Xdir",'reverse');
hold on
scatter(x,y,val*200,val,'filled','Markerfacealpha',0.6);
axis equal
set(gca,'xlim',[0 1]);
set(gca,'ylim',[0 1]);
box on
a12=axes('pos',[0.5 0.5 0.45 0.45],'Yaxislocation','right');
hold on
scatter(z,y,val*200,val,'filled','Markerfacealpha',0.6);
axis equal
set(gca,'xlim',[0 1]);
set(gca,'ylim',[0 1]);
box on
a21=axes('pos',[0.05 0.05 0.45 0.45],"Xdir",'reverse');
hold on
scatter(x,z,val*200,val,'filled','Markerfacealpha',0.6);
axis equal
set(gca,'xlim',[0 1]);
set(gca,'ylim',[0 1]);
box on
aa=1;
end
%==============eof==================
  1 件のコメント
Petr Kolar
Petr Kolar 2025 年 1 月 29 日 9:49
hello,
thanks for the reaction
here is another descritption of the (plot) requirements:
  • origin of all the three subplots should be identical - point [0 0] of subplots corresponding to point [ 0 0 0] of the data (somewhere in the middle of parent fig.)
  • the (lover) axes of subplot 11 should be identical with (upper) axes of subplot 21
  • the (right) axes of subplot 11 should be identical with (left) axes of subplot 12
  • required operation is (mainly) figure size change
It could be done also by the code given below, however the TickLabels do not correspond to the 3D data (in signs). Of course the TicksLabels can be re-write, or 2nd axes could be added .... but is there any way how to place subplots at the particular positon (and size) into pattern figure/axes ? And keep their mutual position/connection while e.g. the fig size change? (the subaxes can be linked).
function plot3x2D_v2
close all
clear all
NN=10;
x=rand(1,NN);
y=rand(1,NN);
z=rand(1,NN);
val=rand(1,NN);
figure; hold on
%axis off
% added two calibration points
x=[x 0 1];
y=[y 0 1];
z=[z 0 1];
val=[val 0.1 0.9];
% subplot 11
scatter(-x,y,val*200,val,'filled','Markerfacealpha',0.6);
% subplot 12
scatter(z,y,val*200,val,'filled','Markerfacealpha',0.6);
% subplot 21
scatter(-x,-z,val*200,val,'filled','Markerfacealpha',0.6);
plot([-1 1],[0 0],'k:');
plot([0 0],[-1 1],'k:');
axis equal
set(gca,'xlim',[-1 1]);
set(gca,'ylim',[-1 1]);
box on
end

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2025 年 1 月 28 日 14:38
To clarify, when you say "position" do you mean the location of the subplot axes in the figure window, the limits of the axes (so that all three are "looking at" the same section of the Cartesian plane), both of those, or something else?
If you mean the locations in the figure window, nothing that I can see in your code ought to change those. The axis equal calls might change how much of the axes "screen real estate" is actually used for the plotting area, but it shouldn't change the Position property of the axes.
ax = axes;
plot(ax, 1:10, (1:10).^2)
posBefore = ax.Position
posBefore = 1×4
0.1300 0.1100 0.7750 0.8150
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
axis equal
posAfter = ax.Position
posAfter = 1×4
0.1300 0.1100 0.7750 0.8150
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
isequal(posBefore, posAfter)
ans = logical
1
If you mean the limits of the axes, so zooming into one zooms into the others and the same with panning, take a look at the linkaxes function.
If you mean something else when you say "position", please explain in more detail what quality of the three axes you want to remain unchanged and what manipulations you're planning to perform on the axes (panning, zooming, changing the view, etc.)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by