Info

此问题已关闭。 请重新打开它进行编辑或回答。

Is it possible to edit a legend with 19 labels down to only 6 labels which correspond to a colour?

1 次查看(过去 30 天)
I want to add a legend to my graph, but when I do it automatically displays all lines of data. What I would like to do is arrange it so all data of the same colour is displayed as one label on the legend.
For example, I want all the data in green ('data2', 'data3', 'data4', 'data5', 'data6', data7') to instead just display -o- as 'segment 2'
I know I can do this by entering the following code:
legend('Segment1', ...
'Segments 2 and 3', '', '', '', '', '', ...
'segments 4 and 5', '', ...
'Segment 6')
Ideally, I'd like to be able to list the 'segments' without the data lines appearing in the legend, so that all you can see is:
- red line - segment 1
- green line - segment 2
- magenta line - segment 3
- blue line - segment 4... and so on.
Is it possible to do this in Matlab? Thanks in advance!

回答(2 个)

Steven Lord
Steven Lord 2020-6-26
You can pass a vector of specific handles that you want to appear in the legend into the legend function.
% Sample data
x = 0:360;
% Setup the axes and a vector of handles
axis([0 360 -1 1])
hold on
h = gobjects(5);
% Plot five sine curves
for k = 1:5
h(k) = plot(x, sind(k*x), 'DisplayName', "sine of " + k + "*x");
end
% Create the legend containing only some of the sine curves
legend(h([1 2 4]))
Note that the entries for the sine of 3*x and the sine of 5*x are not included in the legend.
  2 个评论
MADRobot
MADRobot 2020-6-26
The thing is, I have 19 plots, but I want to somehow assign some of the plots to one label. Is there a way of doing this? Please bear in mind I am currently doing this for a static position, but I actually have over 1000 rows of data with 3 columns per dataset
Steven Lord
Steven Lord 2020-6-26
In that case, group the plots that are in the same "category" together in an hggroup object and specify that in the legend.
% Sample data
x = 0:360;
% Setup the axes and a vector of handles
axis([0 360 -1 1])
hold on
h = gobjects(6);
g = gobjects(3);
for whichGroup = 1:3
g(whichGroup) = hggroup('DisplayName', "Category " + whichGroup);
end
% Plot six sine curves
for k = 1:6
P = mod(k-1, 3)+1;
h(k) = plot(x, sind(k*x), 'DisplayName', "sine of " + k + "*x", 'Parent', g(P));
end
% Create the legend containing only some of the sine curves and categories
legend([g(3), h([1 2])])

Walter Roberson
Walter Roberson 2020-6-26
When what you want to show up in the legend does not correspond to specific objects, such as because you want to group several objects in the same legend, then the classic approach is to create a vector of extra objects with the same characteristics as you want to appear in the legend, but with data values that are non-finite. Objects with non-finite data values are not displayed even though they exist. Then you legend() passing in the vector of extra objects as the first parameter: even though the objects do not show up, their characteristics can be used to draw legends.
h = plot(nan, nan, '-r', nan, nan, '-g', nan, nan, '-m', nan, nan, '-b');
legend(h, {'segment 1', 'segment 2', 'segment 3', 'segment 4'});

此问题已关闭。

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by