Change color of graph at a certain value (temperature vs. time)

11 次查看(过去 30 天)
Hi,
I want to plot temperatures (type double) vs. time (type datetime). When the temperatures are below zero, I want the graph to be blue, and above zero I want it to be red. How can I do this? I know this has been discussed before, but I am struggeling due to the datetime on the x-axis. I still want the x-ticks to show up as dates. The file is attached
Thank you:)
  2 个评论
Adam
Adam 2020-2-24
Just plot it as you would normally, but with two separate operations. Plot only the above zero part and specify it to be red and plot only the below zero part separately and specify it to be red.
e.g.
plot( hAxes, x( y > 0 ), y( y > 0 ), 'r' )
hold( hAxes, 'on' );
plot( hAxes, x( y <= 0 ), y( y <= 0 ), 'b' )
or put 0 in the red section, or plot it its own colour if you prefer. Where hAxes is your axes handle. You can leave that out if you wish to just take your chances as to where things plot, but I almost always include it in my answers.
KNL
KNL 2020-2-24
Thank you. This does however plot two separate graphs, as shown below. I would like it to be only onle graph, but changing between red and blue when crossing 0.

请先登录,再进行评论。

回答(2 个)

the cyclist
the cyclist 2020-2-24
One option would be to draw the data as points, perhaps with a thin black line to guide the eye to time-consecutive points.
rng default
N = 50;
x = 1:N;
y = rand(1,N);
hotThreshold = 0.5;
isHot = y > hotThreshold;
figure
hold on
hH = plot(x(isHot),y(isHot),'r.');
hC = plot(x(~isHot),y(~isHot),'b.');
set([hH,hC],'MarkerSize',36)
plot(x,y,'k')
yline(hotThreshold,'--')
  1 个评论
the cyclist
the cyclist 2020-2-24
If that doesn't look great to you, I think your only other option is to make piecewise colored lines, by looping over your data to see which segments are above/below your threshold.

请先登录,再进行评论。


Star Strider
Star Strider 2020-2-24
This eliminates the connections between the ‘gaps’ in the red and blue areas:
D = load('Lade.mat');
Lade = D.Lade;
Q1 = Lade(1:10,:);
time = Lade.time;
temp = Lade.temp;
temph = temp;
temph(temp <= 0) = NaN;
tempc = temp;
tempc(temp > 0) = NaN;
figure
plot(time, temph, '-r')
hold on
plot(time, tempc, '-b')
hold off
grid
producing this plot:
The only way to fill the gaps around C is to interpolate to a time vector with more points within the same limits. This is your data, so I leave that to you to determine if that is appropriate. Note that interpolating adds data where no data existed previously, so for a plot that may be appropriate, however for data analysis it would not be at all appropriate.

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by