Can I add a text annotation to a tiledlayout figure?

145 vues (au cours des 30 derniers jours)
Charles Dorchester
Charles Dorchester le 19 Mai 2022
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.

Réponse acceptée

David Hill
David Hill le 19 Mai 2022
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

Plus de réponses (1)

David Hill
David Hill le 19 Mai 2022
Look at text function
  1 commentaire
Charles Dorchester
Charles Dorchester le 19 Mai 2022
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

Connectez-vous pour commenter.

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by