Plot a shadow between to lines for a current figure without legend

15 Ansichten (letzte 30 Tage)
Hi, I want to fill with light gray shadow (how to control the color?) between my two vertical line, xline(1), xline(10), the height is just consisent with the figure. and I don't want legend for xline(1) and xline(10) Thank you
figure
le=[1,2,3];
x=[0 5 15];
y=2*x;
for i =1 : length(x)
txt = ['$\alpha_1$ = ',num2str(le(1,i))];
plot(x, y, '-+','LineWidth',1.5,'DisplayName',txt);
hold on
end
legend('Location','best','Interpreter','latex','FontSize',16)
legend show
legend boxoff
hold on
xline(1)
xline(10)
grid on
set(gca,'fontweight','bold','FontSize',20)

Akzeptierte Antwort

Star Strider
Star Strider am 17 Nov. 2022
Bearbeitet: Star Strider am 17 Nov. 2022
Add a patch call —
figure
le=[1,2,3];
x=[0 5 15];
y=2*x;
for i =1 : length(x)
txt = ['$\alpha_1$ = ',num2str(le(1,i))];
hp1{i} = plot(x, y, '-+','LineWidth',1.5,'DisplayName',txt);
hold on
end
hold on
xlinelocs = [1 10];
hxl = xline(xlinelocs);
hp2 = patch([xlinelocs flip(xlinelocs)], [[1 1]*min(ylim) [1 1]*max(ylim)], 'b', 'FaceAlpha',0.25);
hp2.FaceColor = [1 1 1]*0.75;
grid on
set(gca,'fontweight','bold','FontSize',20)
legend([hp1{:}],'Location','best','Interpreter','latex','FontSize',16)
legend show
legend boxoff
Add or modify the patch parameters to get the desired result.
EDIT — Repositioned the legend call.
.
  2 Kommentare
Miraboreasu
Miraboreasu am 17 Nov. 2022
The patch is not flixeable with different data
Star Strider
Star Strider am 17 Nov. 2022
The patch call should work, regardless of what else is plotted with it.
I do not understand the problem you are having with it, and what needs to be fixed.
Please post your code and how you want the plot to appear.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by