Hello there,
I have two parameters that I want to plot them in one graph with area (transparent/alpha mode) for each of them as follow:
Here is my initial script (line plots):
load('data_x.mat')
figure;
plot(x1,y,'b','Linewidth',1);
hold on;
plot(x2,y,'r','Linewidth',1);
set(gca,'xscale','log','Ydir','reverse')
Warning: Negative data ignored
Does anyone know how to do that? Please find attached my data.
Thank you

 Akzeptierte Antwort

Voss
Voss am 14 Mai 2024

0 Stimmen

load('data_x.mat')
figure;
plot(x1,y,'b','Linewidth',1);
hold on;
plot(x2,y,'r','Linewidth',1);
set(gca,'xscale','log','Ydir','reverse')
x_lim = xlim();
Warning: Negative data ignored
x_p = x1;
x_p(~isfinite(x1) | x1 <= 0) = x_lim(1);
patch([x_p x_lim([1 1])],[y y([end 1])],'b','EdgeColor','none','FaceAlpha',0.25)
x_p = x2;
x_p(~isfinite(x2) | x2 <= 0) = x_lim(1);
patch([x_p x_lim([1 1])],[y y([end 1])],'r','EdgeColor','none','FaceAlpha',0.25)

4 Kommentare

Adi Purwandana
Adi Purwandana am 14 Mai 2024
Bearbeitet: Adi Purwandana am 14 Mai 2024
Great. Thank you!
Anyway, do you know how to add legend properly? I put this line but does not looking good...
legend('A','B')
It creates 4 items in the legend instead of two. Any suggestion?
You're welcome!
One way is to set the patches' HandleVisibility to 'off':
load('data_x.mat')
figure;
plot(x1,y,'b','Linewidth',1);
hold on;
plot(x2,y,'r','Linewidth',1);
set(gca,'xscale','log','Ydir','reverse')
x_lim = xlim();
Warning: Negative data ignored
x_p = x1;
x_p(~isfinite(x1) | x1 <= 0) = x_lim(1);
patch([x_p x_lim([1 1])],[y y([end 1])],'b','EdgeColor','none','FaceAlpha',0.25,'HandleVisibility','off')
x_p = x2;
x_p(~isfinite(x2) | x2 <= 0) = x_lim(1);
patch([x_p x_lim([1 1])],[y y([end 1])],'r','EdgeColor','none','FaceAlpha',0.25,'HandleVisibility','off')
legend('A','B')
Adi Purwandana
Adi Purwandana am 14 Mai 2024
Thank you!
Voss
Voss am 14 Mai 2024
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022a

Gefragt:

am 14 Mai 2024

Kommentiert:

am 14 Mai 2024

Community Treasure Hunt

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

Start Hunting!

Translated by