How to centre textboxes on the plot outline?
Show older comments
Hi, I'm trying to create a plot with a couple of textbox annotations. Ideally, I'd like the text boxes to be centred on the boundary of the white plot area - not relative to the figure space. However, I'm struggling to find a way to easily do this - is there a means of finding these dimensions and not the dimensions of the entire figure? My code is as follows:
%Illustrates wavelength vs period
close all;
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 4; % hertz
x =sin(2*pi*Fc*t);
% Plot the signal versus time:
figure;
plot(t,x);
set(gca,'xtick',[])
set(gca,'ytick',[])
annotation('doublearrow',[0.132142857142857,0.903571428571429],[0.923,0.923])
str = {'Period','T'};
dim = [0.457785714285714,0.883333333333335,0.127571428571429,0.0952380952381];
annotation('textbox',dim,'String',str,'FitBoxToText','on','Horizontalalignment','center','BackgroundColor', 'w');
annotation('doublearrow',[0.132142857142857,0.9035714285714291],[0.11,0.11])
str = {'Wavelength','\lambda'};
dim = [0.457785714285714,0.054761904761905,0.127571428571429,0.0952380952381];
annotation('textbox',dim,'String',str,'FitBoxToText','on','Horizontalalignment','center','BackgroundColor', 'w');
hold on;
yline(0);
zoom xon;
I hope what I am trying to achieve is clear from the plot, but as you can see it uses a very convoluted way of achieving a result which still doesn't look right.
Thanks in advance for your time.
Accepted Answer
More Answers (0)
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!