Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab - how to make a custom legend

Tags:

legend

matlab

I have the following picture :

enter image description here

And I would like to make a legend for it. Basically, I want to make a legend for each type of rectangle. In the legend box, I want to mark each color line according to the type of body which it marks:

  • green line : head
  • yellow line : torso
  • purple line : right arm
  • cyan line : left arm
  • red line : left leg
  • blue line : right leg

This is basically custom, because I have more rectangles of each type. How can I do a custom legend and attach it to the figure which draws this picture?

like image 872
Simon Avatar asked Oct 15 '12 11:10

Simon


People also ask

How do I create a custom legend in MATLAB?

Add Custom Legends Using the text() Function in MATLAB You need to pass the x and y coordinate on which you want to place the text. Simply plot the variable and then select the coordinates from the plot and then use the text() function to place the text on the selected coordinates.

How do you change the legend on a figure in MATLAB?

If you double-click on a text label in a legend, MATLAB opens a text editing box around all the text labels in the legend. You can edit any of the text labels in the legend. To access the properties of these text objects, right-click on a text label and select Properties from the context-sensitive pop-up menu.

Can you have multiple legends in MATLAB?

The function LEGEND allows creation of only one legend object per axes. Multiple legends can be created by using the function COPYOBJ to make a copy of a legend object.

What is legend command in MATLAB?

legend creates a legend with descriptive labels for each plotted data series. For the labels, the legend uses the text from the DisplayName properties of the data series. If the DisplayName property is empty, then the legend uses a label of the form 'dataN' .


2 Answers

I would like to add to dvreed77's answer on using hggroup that for clean legend use, I also needed to set the 'IconDisplayStyle' (Matlab R2014a), such that:

%4 kinds of lines:
n_areas = 4;
n_lines = 10;

%use built-in color map
cmap = hsv(n_areas);

%plot lines and generate handle vectors
h_fig = figure;
hold on
h_lines = zeros(1,n_lines);

for l = 1:n_areas

  for k = 1:n_lines
    h_lines(k) = plot(rand(1,2), rand(1,2),'Color',cmap(l,:));
  end

  %Create hggroup and set 'icondistplaystyle' to on for legend
  curPlotSet = hggroup;
  set(h_lines,'Parent',curPlotSet);
  set(get(get(curPlotSet,'Annotation'),'LegendInformation'),...
      'IconDisplayStyle','on');
end

%Now manually define legend label
legend('heads','legs','hands','feet')
like image 28
Maximilian Schulz Avatar answered Oct 13 '22 00:10

Maximilian Schulz


There are 2 ways you could go about this. You could create your squares and then assign them to an hggroup. This way you dont have multiple items for each color. Something like this:

hold on
for ii = 1:4
    hb(ii) = plot(rand(1,2), rand(1,2),'color','r'); 
end

hg = hggroup;
set(hb,'Parent',hg) 
set(hg,'Displayname','Legs')

legend(hg)

Or you could create dummy objects, like this:

hold on
for ii = 1:4
    hb(ii) = plot(rand(1,2), rand(1,2),'color','r'); 
end

p = plot([],[],'r');

legend(p,'Legs')

The former is a little more elegant.

like image 95
dvreed77 Avatar answered Oct 13 '22 00:10

dvreed77