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

15 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

Star Strider
Star Strider il 17 Nov 2022
Modificato: Star Strider il 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 Commenti
Star Strider
Star Strider il 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.

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by