Can I add a text annotation to a tiledlayout figure?

118 次查看(过去 30 天)
I'm plotting data from different folders onto one plot using a tiledlayout configuration. I'd like to add annotations 'A' through 'H' so that I can refer to individual tiles from the plot in my figure caption.
I'm having trouble getting the functions 'text' or 'annotation' to automatically adjust the location of the annotation to the northwest corner of the tile. Is there a way to easily do this, similar to how legend will automatically place the legend in each individual tile through a function call like "legend('blah blah', 'Location','Northwest')"?
Below is my code, where calling the text function like "text(0.03,0.005,subPlotNames(i))" would require manually adjusting the x,y coordinates of the function call and is not flexible if number of tiles are adjusted.
subPlotNames = ["A","B","C","D","E","F","G","H"];
fig = figure(1)
for i = 1:8
cd(ffolders(i))
load plotting_mat_adv.mat
i
t=subplot(2,4,i)
h(:,1) = plot(fluid_conductivity2_adv(1:injection-1,1),...
conductivity_bulk2(1:injection-1,1),'r--x') % injection
hold on
h(:,2) = plot(fluid_conductivity2_adv(flush-1:end,1),...
conductivity_bulk2(flush-1:end,1),'b--x') % flush
% the following line does not work
text(subPlotNames(i),'Location','Northwest')
% something like text(0.03,0.005,subPlotNames(i)) would require
% manually adjusting the x,y coordinates of the function call and is
% not flexible if number of tiles are adjusted.
ylim([0.005 .035]), xlim([0.005, 0.02])
set(gca,'FontSize',12,'FontName','Calibri');
if i == 1 || i == 5
;
else
set(gca,'ytick',[])
end
end
Below is a visual of what I'd like to achieve, where A would proceed to B, C, etc for each tile.

采纳的回答

David Hill
David Hill 2022-5-19
subPlotNames = 'ABCDEFGH';
fig = figure(1);
hold on;
set(gca,'FontSize',12,'FontName','Calibri');
for i = 1:8
x=randi(randi(100)+20,1,10);
y=randi(randi(30)+10,1,10);
subplot(2,4,i)
plot(x,y);
x=randi(100,1,10);
y=randi(30,1,10);
plot(x,y);
xlim(xlim+[-10,10]);ylim(ylim+[-10,10]);%use xlim and ylim to fix location
text(min(xlim), max(ylim),subPlotNames(i), 'Horiz','left', 'Vert','top')
end

更多回答(1 个)

David Hill
David Hill 2022-5-19
Look at text function
  1 个评论
Charles Dorchester
Charles Dorchester 2022-5-19
I have. And my approach to this is explained in the post.
The issue I'm running into: "something like text(0.03,0.005,subPlotNames(i)) would require manually adjusting the x,y coordinates of the function call and is not flexible if number of tiles are adjusted".
Do you know if it is possible (and if so, how to do it) to have the location of the text annotation automatically defined in relation to the tile? The current way sets the text annotation location relative to the entire figure

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by