Info
This question is closed. Reopen it to edit or answer.
Is it possible to edit a legend with 19 labels down to only 6 labels which correspond to a colour?
1 view (last 30 days)
Show older comments
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!
0 Comments
Answers (2)
Steven Lord
on 26 Jun 2020
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 Comments
Steven Lord
on 26 Jun 2020
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
on 26 Jun 2020
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'});
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!