MATLAB: Plotting one curve with two colors

colourscurveplot

Hello friends,
I want to plot my curve from 15.01.2018 until 14.10.2018 with a bright green and from 15.10.2018 until 15.07.2019 with a dark green. Can someone explain to me how can I do this?
Thank you in advance 🙂

Best Answer

Try this:
dnv = datenum({'15.01.2018','14.10.2018','15.10.2018','15.07.2019'}, 'dd.mm.yyyy'); % Limits
dn = dnv(1):dnv(end); % Continuous Dates Vector
s = sin(linspace(0, 2*pi, numel(dn))); % Create Dependent Variable Data
dnd{1} = (dn >= dnv(1)) & (dn <= dnv(2)); % Light Green Limits
dnd{2} = (dn >= dnv(3)) & (dn <= dnv(4)); % Dark Green Limits
figure
plot(dn(dnd{1}), s(dnd{1}), 'Color',[0.1 0.8 0.1], 'LineWidth',2)
hold on
plot(dn(dnd{2}), s(dnd{2}), 'Color',[0.1 0.5 0.1], 'LineWidth',2)
hold off
grid
datetick('x', 'dd.mm.yyyy', 'keepticks')
set(gca, 'XTickLAbelRotation', 30)
Experiment to get the result you want.
Plotting one curve with two colors - 2019 09 20.png