Clear Filters
Clear Filters

loop plot add text to only one specific plot year

10 views (last 30 days)
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

Accepted Answer

Voss
Voss on 21 Jun 2024
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
  4 Comments
SD
SD on 21 Jun 2024
I see you changed my filename as well. I was trying to use spintf with %d but was also trying to incorportate the path and the filename in one code. I gave up and split the path and filename and went to basics. Thanks for the added bonus of code. I will revisit sprintf :)

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 21 Jun 2024
I recommend using xregion (requires R2023a or later) or xline (requires R2018b or later). With xline you can also include a text label if you'd like.
  1 Comment
SD
SD on 21 Jun 2024
Edited: SD on 21 Jun 2024
Hi Adam,
This does not solve my first problem but I was going to work on highlighting specific years where the temperature warms up. I am great full and now I know what I am looking for. I will check this out a little furthur. Weekend task :)

Sign in to comment.

Categories

Find more on Graphics Objects 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!