create a legend that different marks in one line

5 ビュー (過去 30 日間)
Shun Wang
Shun Wang 2024 年 9 月 19 日
コメント済み: Shun Wang 2024 年 9 月 30 日
hello i saw someone create a legend like this, how can i do the same things.It show diffferent marks in one line,and use the same text to describe it.
  2 件のコメント
Shun Wang
Shun Wang 2024 年 9 月 30 日
Thanks,It is what i'm looking for

サインインしてコメントする。

回答 (1 件)

Rahul
Rahul 2024 年 9 月 25 日
Hi,
In case you’re trying to merge two labels in a given row of a legend object, while drawing multiple plots, you can try using the ‘Legend’ object properties to create icons and labels in the same line of the figure’s legend block. Here’s a possible solution to the issue you’re facing:
  • To generate plot data, initialize an empty figure and plot multiple lines using ‘hold’ command, with different styles.
% Plot OP's code
figure; hold all; % new fig, enable hold/add
x=1:5;
y=x;
hL(1) = plot(x,y,'ob');
hL(2) = plot(x,y+1,'r-s');
  • Create a legend object with only one label, and position to the top right.
% Add legend for the first/main plot handle
hLegend = legend(hL,'sample1');
Warning: Ignoring extra legend entries.
hLegend.Position = [0.6 0.8 0.3 0.01];
drawnow(); % have to render the internal nodes before accessing them
  • Using obtained legend handle ‘hLegend’, get children of ‘LabelEntry’ object which consists of ‘Maker’ icons and ‘Linestrip’(s), and assign a string label to the legend entry.
%%% Update top row legened (blue circle)
% Extract legend nodes/primitives
hLegendEntryTop = hLegend.EntryContainer.NodeChildren(end); % top row of legend
hLegendEntryTop.Label.String = 'First, Second';
iconSet1 = hLegendEntryTop.Icon.Transform.Children.Children; % array of first/bottom row's icons (marker+line)
  • Marker Object or Icon label for the 2nd plot ‘newLegendIcon1’, can be created by copying the existing icon object ‘iconSet1(1)’ (correspondingly a new Linestrip object can be copied using ‘iconSet1(2)’ for specifying line along with marker) ’, and assigned a marker style and position, same as the plot handle ‘hL(2)’ ‘Linestyle’ property.
% Move primary marker over to the edge
iconSet1(1).VertexData(1) = -0.5;
% % Create a new icon marker to add to the icon set
newLegendIcon1 = copy(iconSet1(1)); % copy the object (or look into making a matlab.graphics.primitive.world.Marker)
newLegendIcon1.Parent = iconSet1(1).Parent; % set the parent, adding to the legend's icon draw set
newLegendIcon1.Style = hL(2).Marker;
newLegendIcon1.EdgeColorData = 255*uint8([hL(2).Color'; 1]);
newLegendIcon1.VertexData(1) = 0.5;
This creates a single Legend Entry with two markers, used as in the given plots stored in handle array ‘hL’. This can be extended to a greater number of plots, by repeating the last two sections and adding another string while creating legend object.
For more information regarding usage of functions and parameters mentioned above, refer to the documentations below:
  1. Create & Modify Legend Object: https://www.mathworks.com/help/matlab/ref/legend.html#:~:text=lgd%20%3D%20legend(%22First%22%2C%22Second%22%2C%22Third%22%2C%22Fourth%22)%3B
  2. Access Marker Properties: https://www.mathworks.com/help/matlab/creating_plots/create-line-plot-with-markers.html#bvcbmlw-1
  1 件のコメント
Shun Wang
Shun Wang 2024 年 9 月 30 日
Thanks for your specilized answer.I have to say its really complicated to achieve it on matlab.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by