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

15 次查看(过去 30 天)
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)

采纳的回答

Star Strider
Star Strider 2022-11-17
编辑:Star Strider 2022-11-17
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 个评论
Star Strider
Star Strider 2022-11-17
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by