fill in between horizontal lines
14 views (last 30 days)
Show older comments
I have my data graphed (Wetbulb Temperature vs. Time) and i would like to shade in certain regions of the grpah to represent as the temperature drops it moves into certain colored regions of the graph. Ive created horizontal lines at each point i would like to shade a different color but i cant seem to figure out how to fill in each region. Here is my code for my whole project. I will also attach the figure i get when i run the program

%% Clear windows
clear all
clc
%% load data
array1 = load ('prototype0.txt'); %load data from arduino sensors from a txt file
%% Define Variables from sensor data
Humidity= array1(:,1); %defines humidity from column 1 of array1
Pressure=array1(:,2); %defines Pressure from column 2 of array1
Altitude=array1(:,3); %defines Altitude from column 3 of array1
Temperature=array1(:,4); %defines Temperature from column 4 of array1
TimeMili=array1(:,5); %defines Time from column 7 of array1
Dewpoint=Temperature-((100-Humidity)/5);
Wetbulb=Temperature-((Temperature-Dewpoint)/3);
Time=TimeMili/1000;
green=20+Time*0;
yellow=28+Time*0;
red=100+Time*0;
base=0+Time*0;
greenarea=[base,green];
basearea=[base,base]
figure(5)
plot(Time,Wetbulb);
title('Wetbulb Temperature vs. Time');
xlabel('Time');
ylabel('Wetbulb Temperature');
axis([0 200 0 100]);
hold on
plot(Time,green)
plot(Time,yellow)
plot(Time,red)
fill(basearea,greenarea,'g')
0 Comments
Accepted Answer
Star Strider
on 11 Apr 2019
Try this:
Time = 0:200;
Wetbulb = 0.005*(Time-100).^2 + rand(size(Time))*2 + 50;
green=20+Time*0;
yellow=28+Time*0;
red=100+Time*0;
base=0+Time*0;
greenarea=[base,fliplr(green)];
basearea=[Time,fliplr(Time)];
figure(5)
plot(Time,Wetbulb);
title('Wetbulb Temperature vs. Time');
xlabel('Time');
ylabel('Wetbulb Temperature');
axis([0 200 0 100]);
hold on
plot(Time,green)
plot(Time,yellow)
plot(Time,red)
fill(basearea,greenarea,'g', 'FaceAlpha',0.2)
fill(basearea,[green fliplr(yellow)],'y', 'FaceAlpha',0.2)
fill(basearea,[yellow fliplr(red)],'r', 'FaceAlpha',0.2)
hold off
To get patch and fill to work as you want them to, you need to create a closed area, then fill it. I did this here by taking the x-vector for each segment and horizontally concatenating it with a flipped version of itself. (That is the same for all segments.) The y-vector for the green area was a vector of zeros horizontally concatenated with the flipped upper limit. The y-vector for the yellow area was the upper border of the green area horizontally concatenated with the flipped upper border of the yellow area. The same idea applies to the red area. I know your lines are constant and horizontal, so flipping them here does not really matter, however that may not always be the situation (for example with varying borders), so I prefer to use that convention for all patch and fill calls in my code.
I do not have your data, so I created a ‘Wetbulb’ vector. This code should work with your data with little or no alteration, if I understand correctly what you want. I also produced ‘shading’, not solid colours, since you mentioned that. If you want solid colours, you will have to put this line last:
plot(Time,Wetbulb)
so it overplots the red fill region. Otherwise the red fill will hide it.
Experiment to get the result you want.
2 Comments
Star Strider
on 11 Apr 2019
My pleasure.
If my Answer helped you solve your problem, please Accept it!
More Answers (1)
Adam Danz
on 30 Jun 2023
yregion(0,20,'FaceColor','g','EdgeColor','k')
yregion(20,28,'FaceColor','y','EdgeColor','k')
yregion(28,100,'FaceColor','r','EdgeColor','k')
0 Comments
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!