loop plot add text to only one specific plot year
2 次查看(过去 30 天)
显示 更早的评论
Background:
time series for temp and chlorophyll from 2006 to current
created subplots for annual plots (four plots and text in the 5th subplot) (2006, 2007, 2008....2024)
saved title with the specific year for each plot (used num2str)
saved to correct path with the year in the filename (used num2str) filename = ['AnnualPlots ', num2str(datedata), '.jpg'], datedata;
filenames are AnnualPlots 2006.jpg, AnnualPlots 2007.jpg, ...AnnualPlots 2024.jpg
Everything checks out....
We had a sampling pause in 2020 and 2021...you know why. I would like to label this in the 2020 plot and 2021 plot.
???Is it possible to add details to an individual plot within a loop like code??? This is for outreach and I have limited coding experience.
% extract date, total chla, 3µm chla, 1 to 3 µm chla, > 3 µm chla
date = Data.DATEPST; % date sampled
sampletemp = Data.Sample_Temp; % deg C
chlaTotal = Data.TChla; % µg/L
chla3um = Data.Chla3um;% µg/L
chla3um1 = Data.Chla3um1; % 3µm>Chla percent(%)
chla3um2 = Data.Chla3um2; % 3µm<Chla percent(%)
% Get the current year
currentYear = year(datetime('now'));
% Iterate over each year from 2006 to the current year
for datedata = 2006:currentYear
% Create a new figure for each year
figure;
set(gcf, 'Position', [10,10,800,600])
subplot(5,1,1);
plot(date,sampletemp,"color","#4682B4", "LineWidth",0.5); % #4682B4 steal blue
yyaxis left
title({[num2str(datedata) ' Coastal Water (10 m depth)'] 'California'});
ylabel('Temp (^oC)')
xlim([datetime(datedata,1,1,0,0,0)...
datetime(datedata,12,31,0,0,0)])
datetick('x','mmm','keeplimits','keepticks')
ylim([6 29])
where would this go and how to relate it to AnnualPlots 2020.jpg???
txt1 = '- - - - anthropause - - - -';
text(x,3,txt1,'Color','red','FontSize',10,'Rotation',0);
%yyaxis default was red; following code changes it back to black
ax = gca;
ax.YColor = 'k';
subplot(5,1,2);
...
subplot(5,1,3);
...
subplot(5,1,4);
...
subplot(5,1,5);
...
% Save each figure to a specific folder
filepath = 'C:\Me\Desktop\website figures';
filename = ['AnnualPlots ', num2str(datedata), '.jpg'], datedata;
saveas(gcf, fullfile(filepath,filename));
end
0 个评论
采纳的回答
Voss
2024-6-21
If I understand the task correctly, you can use an if condition to create the text object only when datedata is 2020 or 2021.
Example:
filepath = 'C:\Me\Desktop\website figures';
% Iterate over each year from 2006 to the current year
for datedata = 2006:currentYear
% Create a new figure for each year
figure;
set(gcf, 'Position', [10,10,800,600])
% make plots
% ...
subplot(5,1,1);
% ...
subplot(5,1,2);
% ...
subplot(5,1,3);
% ...
subplot(5,1,4);
% ...
subplot(5,1,5);
% ...
% make text in subplot 5 for 2020/2021
% [if you want the text in another subplot, move this block up to the
% appropriate place in the code or capture the subplot axes handle,
% e.g., ax=subplot(5,1,2), and then create the text in that axes, i.e.,
% text(ax,x,3,...)]
if datedata == 2020 || datedata == 2021
x = 0; % specify the x-location of the text
txt1 = '- - - - anthropause - - - -';
text(x,3,txt1,'Color','red','FontSize',10,'Rotation',0);
end
% Save each figure to a specific folder
filename = sprintf('AnnualPlots %d.jpg',datedata);
saveas(gcf, fullfile(filepath,filename));
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!