フィルターのクリア

How can I draw a frame around the inset plot?

8 ビュー (過去 30 日間)
Sim
Sim 2024 年 9 月 2 日
コメント済み: Sam Chak 2024 年 9 月 2 日
How can I draw a frame around the inset plot?
x1 = linspace(0,1);
x2 = linspace(3/4,1);
y1 = sin(2*pi*x1);
y2 = sin(2*pi*x2);
figure(1)
% plot on large axes
plot(x1,y1)
% create smaller axes in top right, and plot on it
axes('Position',[.6 .6 .2 .2])
box on
plot(x2,y2)
My desired output is the following one:

採用された回答

Star Strider
Star Strider 2024 年 9 月 2 日
Use an annotation object —
x1 = linspace(0,1);
x2 = linspace(3/4,1);
y1 = sin(2*pi*x1);
y2 = sin(2*pi*x2);
figure(1)
% plot on large axes
plot(x1,y1)
Ax1 = gca;
pos1 = Ax1.Position;
% create smaller axes in top right, and plot on it
Ax2 = axes('Position',[.6 .6 .2 .2]);
box on
plot(x2,y2)
pos2 = Ax2.OuterPosition;
a2 = annotation('rectangle', pos2);
a2.Color = 'r';
a2.LineWidth = 2;
.
  7 件のコメント
Sim
Sim 2024 年 9 月 2 日
Very clear, thanks a lot! To be honest, I got the same feeling about a possible AI anwer... OK, lets do as suggested... I accept one answer and I hope people can upvote the ather one to equally recognise both answers... :-)
Sam Chak
Sam Chak 2024 年 9 月 2 日
I would evaluate which solution provides a more aesthetically pleasing red frame that closely matches the reference. Let @Sim decide.

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

その他の回答 (1 件)

Jaimin
Jaimin 2024 年 9 月 2 日
Hi @Sim
Based on the description, I understand that you want to add a frame around the inset plot in the figure, as shown below.
Below is the sample code to meet the requirement.
x1 = linspace(0,1);
x2 = linspace(3/4,1);
y1 = sin(2*pi*x1);
y2 = sin(2*pi*x2);
figure(1)
% Plot on large axes
plot(x1, y1)
% Create smaller axes in top right, and plot on it
inset_axes = axes('Position', [.6 .6 .2 .2]);
plot(x2, y2)
% Get the current position of the inset axes
pos = inset_axes.Position;
% Adjust the position to make the rectangle slightly larger
margin = 0.01; % Adjust this value to change the size of the margin
new_pos = [pos(1) - margin, pos(2) - margin, pos(3) + 2*margin, pos(4) + 2*margin];
% Add a red box around the inset plot
annotation('rectangle', new_pos, 'Color', 'r', 'LineWidth', 2);
Please refer this MathWorks Documentation to understand “annotation”
Annotation:
I hope this will be helpful.
  1 件のコメント
Sim
Sim 2024 年 9 月 2 日
編集済み: Sim 2024 年 9 月 2 日
Thanks @Jaimin! I would like to accept both your's and @Star Strider answer.. And following the @Image Analyst suggestions, I accepted the @Star Strider answer and I asked people to upvote yours, so that you will be equally awarded as @Star Strider (i.e. with the same score) :-)

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by