highlight different data on the same graph
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I create a plot with two different axes
figure
yyaxis left
plot(Tclean.x,Tclean.Temperature,'b-')
ylabel('°t')
yyaxis right
plot(Tclean.x,Tclean.ph,'r-')
ylabel('ph')
Datas come from a table that include temperature, DO and x is a time vector ('2021-07-21 02:10:00' - Format: yyyy-MM-dd HH:mm:ss).
x axis start at 2021-07-21 00:00:00 and it ends at 2021-07-23 08:40:00.
I'd like to highlight a period (from 2021-07-21 02:10:00 to 2021-07-21 08:40:00) on the same graph with a different line.
I thought to creat a loop
hold on
for i = {'2021-07-21 02:10:00'}:{'2021-07-21 08:40:00'}
plot(Tclean.x(i),Tclean.Temperature,'bo')
end
but the output is
%Undefined function 'colon' for input arguments of type 'cell'
0 commentaires
Réponses (2)
Star Strider
le 19 Août 2021
Try something like this:
Tclean = table(datetime('2021-07-21 00:00:00')+minutes(0:10:1000).', 'VariableNames',{'Time'}); % Create Table
Tclean = [Tclean array2table(rand(height(Tclean), 4))] % Create Table
iv = [datetime('2021-07-21 02:10:00') datetime('2021-07-21 08:40:00')] % Limits
TcleanVct = (Tclean{:,1} >= iv(1)) & (Tclean{:,1} <= iv(2)); % Logical Vector
figure
plot(Tclean{:,1}, Tclean{:,2}, '-b')
hold on
plot(Tclean{TcleanVct,1}, Tclean{TcleanVct,2}, '--r')
hold off
grid
Make appropriate changes toi get the result you want.
.
0 commentaires
Adam Danz
le 19 Août 2021
Modifié(e) : Adam Danz
le 19 Août 2021
It looks like you're using datetime values and if you're not, you should be. Datetime values are not represented by strings. A datetime loop loops through days but your interval is less than a day so it will only execute for the first datetime value. Were you expected it to loop through hours / minutes / or seconds?
Why not just add a colored rectangled that spans the vertical extent of the plot between your selected dates? That will avoid precision errors.
dt = datetime('2021-07-21') + hours(0:24);
data = rand(2,numel(dt));
figure()
yyaxis left
h1 = plot(dt, data(1,:), 'o','DisplayName','LeftData');
yyaxis right
h2 = plot(dt, data(2,:), 'o', 'DisplayName', 'RightData');
interval = datetime({'2021-07-21 02:10:00','2021-07-21 08:40:00'});
yyaxis left
hold on
ax = gca();
ph = patch([interval,fliplr(interval)], ax.YLim([1 1 2 2]), 'k', ...
'FaceAlpha', .1, 'EdgeColor','none', 'DisplayName','Selection');
legend([h1,h2,ph],'Location','SouthOutside', 'orientation','horizontal')
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!