Color background between two ylines patch (datetime)

I have a plot with heartrate over time, using a datetime function. In addition I have two ylines (y=60 and y=100). Now I want to color the area (background) between those y-lines, for my whole timeline using datetime. The function patch does not work, as it cannot deal with datetime functions. How can I do this?

1 Kommentar

Geoff Hayes
Geoff Hayes am 15 Mär. 2019
Lieke - can you provide a screen shot of what you have and what you want coloured in? I don't understand how the datetime function is an issue since you just want to color the area between those y-lines. Aren't the y-lines just y=60 and y=100?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Star Strider
Star Strider am 15 Mär. 2019
The error is:
Error using patch
Non-numeric data is not supported in 'patch'
So you can’t use it with datetime objects.
If you want a single vertical line, just use plot:
datetime1=datetime('18-Aug-2017 22:56:00');
datetime2=datetime('18-Aug-2017 23:56:00');
figure
plot([datetime1 datetime1],[50 100],'red')
ylim([40 150])
set(gca, 'XTickLabelRotation',45)
Experiment to get the result you want.

6 Kommentare

Lieke Numan
Lieke Numan am 15 Mär. 2019
Thanks! But I want to color the whole space between those two lines, instead of only using colored lines. Does anyone know another function that does work within a datetime plot?
My pleasure!
If you want to use patch, you can’t use datetime objects.
The fill (link) function works with datetime objects:
datetime1=datetime('18-Aug-2017 22:56:00');
datetime2=datetime('18-Aug-2017 23:56:00');
figure
fill([datetime1 datetime2 datetime2 datetime1],[50 50 100 100],'red')
ylim([40 150])
set(gca, 'XTickLabelRotation',45)
Experiment to get the result you want.
Lieke Numan
Lieke Numan am 15 Mär. 2019
Thanks a lot! That works perfectly.
Do you also know how I can use transparent colors for the filled area?
My pleasure!
You can vary the transparency iwth the 'FaceAlpha' property:
fill([datetime1 datetime2 datetime2 datetime1],[50 50 100 100],'red', 'FaceAlpha',0.5)
See the documentation section on Transparency (link) for details.
The interesting aspect of all this is that fill creates a patch object, and it works with datetime objects by design.
From the documentation:
fill(X,Y,C) creates filled polygons from the data in X and Y with vertex color specified by C. ... The values in X and Y can be numeric, datetime, duration, or categorical values.’
Lieke Numan
Lieke Numan am 18 Mär. 2019
That works perfectly, thanks!
My pleasure.
If my Answer helped solve your problem, please Accept it!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Lieke Numan
Lieke Numan am 15 Mär. 2019

0 Stimmen

I want to color the space between y=60 and y=100
datetime1=18-Aug-2017 22:56:00
datetime2=18-Aug-2017 23:56:00
This is what I wanted to do:
patch([datetime1 datetime2 datetime2 datetime1],[50 50 100 100],'red')
It also doesn't work when I use datenum(datetime1) within the patch function.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by