fill in between horizontal lines

조회 수: 11 (최근 30일)
Ernest Roisch
Ernest Roisch 2019년 4월 11일
답변: Adam Danz 2023년 6월 30일
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
wetbulbgraph.PNG
%% 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')

채택된 답변

Star Strider
Star Strider 2019년 4월 11일
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
Ernest Roisch
Ernest Roisch 2019년 4월 11일
Thank you very much this worked perfectly
Star Strider
Star Strider 2019년 4월 11일
My pleasure.
If my Answer helped you solve your problem, please Accept it!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2023년 6월 30일
MATLAB R2023a introduced yregion which offers an easy way to fill between horizontal lines.
yregion(0,20,'FaceColor','g','EdgeColor','k')
yregion(20,28,'FaceColor','y','EdgeColor','k')
yregion(28,100,'FaceColor','r','EdgeColor','k')

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by