How to align both vertically and horizontally annotation box with legend box in plots?
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Giuseppe
am 1 Okt. 2022
Kommentiert: Star Strider
am 9 Okt. 2022
I want to get the horizontal and vertical alignment of annotation box with the legend box to give simmetry to my plots.
Here is a reproducible example of my typical code I use to get these kind of plots:
clc;clear all; close all;
x= linspace(0,2*pi,1000);
y= sin(x-2);
plot(x,y)
legend('y=sin(x-2)');
grid on;
% Extra info box
dim = [.168 .80 .20 .06];
str = 'info box';
annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','on',...
'BackgroundColor',[1 1 1],'FontSize',9);
Look at the following figure to better consider my problem: I want that the distances represented by the red segment are equal, and the same for the distances represented by the blue segments.
Can you help me to get these alignements?
Is there a way to give to "annotation" the same properties of legend command as "north, east, northwest, etc."?
5 Kommentare
Star Strider
am 9 Okt. 2022
My pleasure!
I was referring to R2022a updates (top toolstrip: RESOURCES —> Help —> Check for Updates), however I definitely recommend R2022b.
I have rarely had problems with MATLAB interim updates, and upgrades to new releases. A few years ago, one new release failed to import my preferences and that required about an extra half hour of work to get those imported, however the others have gone seamlessly.
Akzeptierte Antwort
Simon Chan
am 1 Okt. 2022
"annotation" dose not have properties of legend command as north, east,...
I think you may need to calculate the margin separately as follows:
x= linspace(0,2*pi,1000);
y= sin(x-2);
plot(x,y)
ax=gca;
lgd=legend('y=sin(x-2)');
grid on;
% Extra info box
dim = [.168 .80 .20 .06];
str = 'info box';
an=annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','on',...
'BackgroundColor',[1 1 1],'FontSize',9);
%% Calculate the required margins
newx = ax.InnerPosition(1)+ax.InnerPosition(3)-(lgd.Position(1)+lgd.Position(3))+ax.InnerPosition(1);
newy = lgd.Position(2)+lgd.Position(4)-an.Position(4);
set(an,'Position',[newx newy an.Position(3) an.Position(4)]);
5 Kommentare
Simon Chan
am 8 Okt. 2022
I try to add "axis equal" and the result looks aceptable (Visually check).
I suggest to use "normalized" instead of "pixels" as the Units for all objects incuding the axis itself.
x= linspace(0,2*pi,1000);
y= sin(x-2);
plot(x,y)
ax=gca;
axis(ax,'equal'); % <--Axis equal added here
lgd=legend('y=sin(x-2)');
grid on;
% Extra info box
dim = [.168 .80 .20 .06];
str = 'info box';
an=annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','on',...
'BackgroundColor',[1 1 1],'FontSize',9);
%% Calculate the required margins
newx = ax.InnerPosition(1)+ax.InnerPosition(3)-(lgd.Position(1)+lgd.Position(3))+ax.InnerPosition(1);
newy = lgd.Position(2)+lgd.Position(4)-an.Position(4);
set(an,'Position',[newx newy an.Position(3) an.Position(4)]);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Title finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!