How can I fill the area under the data fully?

8 views (last 30 days)
Ok so my code is below, I would like the red highlighted region in the second graph to be the entirety of the area above the threshold and below the data instead of just a few scattered lines.
clear;
E1 = readtable("Exp 1 - 0-1_C04.txt");
E1 = E1(E1.Var3 == 2, :);
E1 = E1{:,:};
EX1 = E1(:,2);
EY1 = E1(:,1);
EX1RHE = EX1+(0.197+0.059*.3);
figure
plot(EX1RHE,EY1,'-*')
grid on
title('HUPD Method Argon')
xlabel('V vs. RHE')
ylabel('I/mA')
% Assign the y-value threshold to a variable. Makes is clearer to see how and where it is used,
% as well as makes is easy to change if needed.
t = .2;
% Extract x- and y-data for frequencies in the desired range.
EX1A = EX1RHE(EX1RHE>0 & EX1RHE<0.3);
EY1A = EY1(EX1RHE>0 & EX1RHE<0.3)-t;
% Set all values that are <0 (these were originally all values less than the threshold) to zero.
EY1A(EY1A<0) = 0;
% Use trapz function to calculate the area under the curve.
areaint = trapz(EX1A, EY1A)
areaint = 0.2900
figure
plot(EX1RHE,EY1,'-*')
grid on
hold on
title('HUPD Method Argon')
xlabel('V vs. RHE')
ylabel('I/mA')
yline(t,'k--',{'Double Capacitance','Threshhold'},'LabelHorizontalAlignment','left','LabelVerticalAlignment','middle');
ind = (EY1>t) & (EX1RHE<0.3);
EY1(~ind) = NaN;
area(EX1RHE,EY1,t,'EdgeColor','none','FaceColor','r','ShowBaseLine','off')
hold off

Accepted Answer

Star Strider
Star Strider on 28 Mar 2025
I am more familiar with patch than area, so I went with what I’m good at.
I replaced the area call with:
Lv = ~isnan(EY1);
patch([EX1RHE(Lv); flip(EX1RHE(Lv))], [zeros(size(EY1(Lv)))+t; flip(EY1(Lv))], 'r', EdgeColor='none', FaceAlpha=0.5, EdgeAlpha=0)
Tweak it to get the result you want.
Try this —
clear;
E1 = readtable("Exp 1 - 0-1_C04.txt");
E1 = E1(E1.Var3 == 2, :);
E1 = E1{:,:};
EX1 = E1(:,2);
EY1 = E1(:,1);
EX1RHE = EX1+(0.197+0.059*.3);
figure
plot(EX1RHE,EY1,'-*')
grid on
title('HUPD Method Argon')
xlabel('V vs. RHE')
ylabel('I/mA')
% Assign the y-value threshold to a variable. Makes is clearer to see how and where it is used,
% as well as makes is easy to change if needed.
t = .2;
% Extract x- and y-data for frequencies in the desired range.
EX1A = EX1RHE(EX1RHE>0 & EX1RHE<0.3);
EY1A = EY1(EX1RHE>0 & EX1RHE<0.3)-t;
% Set all values that are <0 (these were originally all values less than the threshold) to zero.
EY1A(EY1A<0) = 0;
% Use trapz function to calculate the area under the curve.
areaint = trapz(EX1A, EY1A)
areaint = 0.2900
figure
plot(EX1RHE,EY1,'-*')
grid on
hold on
title('HUPD Method Argon')
xlabel('V vs. RHE')
ylabel('I/mA')
% yline(t,'k--',{'Double Capacitance','Threshhold'},'LabelHorizontalAlignment','left','LabelVerticalAlignment','middle');
ind = (EY1>t) & (EX1RHE<0.3);
EY1(~ind) = NaN;
% area(EX1RHE,EY1,t,'EdgeColor','none','FaceColor','r','ShowBaseLine','off')
Lv = ~isnan(EY1);
patch([EX1RHE(Lv); flip(EX1RHE(Lv))], [zeros(size(EY1(Lv)))+t; flip(EY1(Lv))], 'r', EdgeColor='none', FaceAlpha=0.5, EdgeAlpha=0)
hold off
yline(t,'k--',{'Double Capacitance','Threshhold'},'LabelHorizontalAlignment','left','LabelVerticalAlignment','middle');
.
  2 Comments
Nicholas
Nicholas on 28 Mar 2025
Thank you very much, I have never used patch before but your solution looks great.
Star Strider
Star Strider on 28 Mar 2025
As always, my pleasure!
I’m certain that area calls patch, and my patch call arguments may actually work with area as well. It’s just easier for me to use patch.
The patch function requires a closed region (although it will define the last boundary itself, so that doesn’t need to be specifically stated). That usually requires concatenating the original and flipped versions (in this instance vertically concatenating them, since they’re column vectors) of the x-coordinates (here [EX1RHE(Lv); flip(EX1RHE(Lv))]) and different vectors of the y-coordinates to define the upper and lower limits of the patch (here [zeros(size(EY1(Lv)))+t; flip(EY1(Lv))]). The logical vector ‘Lv’ limits the vectors to the region-of-interest in this instance. The only other required argument is the colour. The other arguments simply modify the way the patch object plots and how it looks.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Spline Postprocessing 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!