hold on y-axis

5 Ansichten (letzte 30 Tage)
GIOVANNA GUADAGNIN
GIOVANNA GUADAGNIN am 11 Mai 2021
i want to plot axes parallel to y-axis. I got the limits for y easily and
on the x-axis I have dates like '2020-11-17 13:03:00'.
this is my plot
figure
yyaxis left
plot(T.UTC_Date___Time,mareaCM,'b--');
ylim
yyaxis right
plot(T.UTC_Date___Time,T.DissolvedOxygenSaturation,'r--');
hold on
%I tried with this but it doesn't work
plot([2020-11-17 13:03:00 2020-11-17 13:03:00],[0 450],'g--')
  1 Kommentar
Andy
Andy am 13 Mai 2021
Do any error messages appear or is it that Matlab doesn't report a problem but the line doesn't appear where you think it should?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 13 Mai 2021
%I tried with this but it doesn't work
% plot([2020-11-17 13:03:00 2020-11-17 13:03:00],[0 450],'g--')
No, that's not going to do what you expected. If you want to plot using date and time data on the X axis, turn that data into a datetime array first.
s = ['2020-11-17 13:03:00'; '2020-11-17 13:03:00']
s = 2×19 char array
'2020-11-17 13:03:00' '2020-11-17 13:03:00'
dt = datetime(s)
dt = 2×1 datetime array
17-Nov-2020 13:03:00 17-Nov-2020 13:03:00
In this case using the xline function is probably what you want instead of plotting using the same X values twice. That will keep the line spanning the whole height of the axes even if you pan or change the Y axis limits.
xline(dt(1), 'g--')

Community Treasure Hunt

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

Start Hunting!

Translated by