How to group xlines in legend

11 次查看(过去 30 天)
I'm trying to plot various vertical lines on my graph. They all represent aspects of the same feature and only need one entry in the legend. This is the code I wrote (that seemed to work a few days ago):
ax = axes
p = xline(ax, [1 2 3], "g-");
hg = hggroup;
set(p, "Parent", hg);
hg.DisplayName = "My Data";
set(get(get(hg,'Annotation'),'LegendInformation'),'IconDisplayStyle','on');
However, when I run this now I get an error
"Error using matlab.graphics.chart.decoration.ConstantLine/setParentImpl - Parent must be a Cartesian axes."
I've read that ConstantLine objects like xline() can't have a Group object as a parent, unike plot(). My code works if I use plot(), but I'm not sure how to effectivly implement vertical lines using that function.

采纳的回答

Star Strider
Star Strider 2023-10-18
Using plot to plot the vertical lines is straightforward —
ax = axes;
% p = xline(ax, [1 2 3], "g-");
p = plot([1;1]*[1 2 3], ([1; 1; 1]*ylim).', 'g-');
hg = hggroup;
set(p, "Parent", hg);
hg.DisplayName = "My Data";
set(get(get(hg,'Annotation'),'LegendInformation'),'IconDisplayStyle','on');
.

更多回答(0 个)

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by