highlight different data on the same graph
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Star Strider
2021-8-19
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 个评论
Adam Danz
2021-8-19
编辑:Adam Danz
2021-8-19
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!