Thread Subject: Categories in Plot legend

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 6 Oct, 2008 14:40:19

Message: 1 of 8

Hello,

I know many people ask questions about legends but I cannot seem to find an answer to this one. I have a plot with 6 different data lines on it; each line belongs to one of two categories (indicated by different colours, markers etc). What would be great is if the legend indicated these categories, i.e. above the first three legend entries it said e.g. "category A" and above the next three it said e.g. "category B".

So far, I enter some additional dummy data into the plot, its colour is set to white and I use this legend entry to provide the category title. However, as you might expect, the title is aligned with the other legend entries and needs to be moved left (see my minimal working example below).

Anyone any ideas on this or could suggest an alternate approach? My other approach was using multiple axes, but as HOLD changes the axes `on-the-fly' to fit the data, getting the two to match seemed awkward.

Thanks,

Chris

% MWE Code:

figure; hold all; axis square; box on;
plot(1,1,'w') % Dummy Data
plot(0:0.2:0.6,4:-1:1,'--^b');
plot(0:0.2:0.6,5:-1:2,'--ob');
plot(0:0.2:0.6,6:-1:3,'--pb');
plot(1,1,'w') % Dummy Data
plot(0:0.2:0.6,7:-1:4,'-^k')
plot(0:0.2:0.6,8:-1:5,'-ok')
plot(0:0.2:0.6,9:-1:6,'-pk')
hold off
set(gca,'xlim',[-0.02 0.62],'xtick',[-0.02 0 0.2 0.4 0.6 0.62],'xticklabel',{'' '0' '0.2' '0.4' '0.6' ''});
xlabel('X');
ylabel('Y');
legend('Category A:','1','2','3','Category B:','1','2','3','location','eastoutside');

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 6 Oct, 2008 21:16:01

Message: 2 of 8

Bump

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 6 Oct, 2008 21:27:03

Message: 3 of 8

Bump

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 7 Oct, 2008 09:15:08

Message: 4 of 8

Bump

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 13 Oct, 2008 13:15:03

Message: 5 of 8

Ok, so after some digging I discovered that each entry in a legend has a handle - they are the 'children' of the legend handle. After some more reading, it seems that there is a handle for each marker, each line and each text entry within the legend. Text entries have a 'position' property which is a 3-element vector which, from I can gather, is [left top ??] - I have no idea what the last element does as changing it appears to have no effect. Anyway, adjusting the first position element in the handle of each of my category titles has the desired effect. However, the legend is now too wide (there is lots of white space) and I have been unable to make it narrower - either in script or using the GUI. I think it is due to the 'extent' of the position-adjusted text entries but cannot be sure. Anyone any ideas?

%%%%%%%%%%%%%

% Code for moving entries:
l_all = get(l1,'children'); % l1 is the legend handle
position = zeros(2,3);
for i = 1:2
    n = [12 24]; % Choose the handles for the categories
    position(i,:) = get(l_all(n(i)),'position');
    position(i,1) = 0.05;
    set(l_all(n(i)),'position',position(i,:))
end

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 13 Oct, 2008 13:38:01

Message: 6 of 8

sorry, the position array should be [left bottom ??]...

Subject: Categories in Plot legend

From: Steven Lord

Date: 13 Oct, 2008 13:58:11

Message: 7 of 8


"Christopher Hummersone" <wheely_chairs@hotmail.com> wrote in message
news:gcvhkn$bh9$1@fred.mathworks.com...
> Ok, so after some digging I discovered that each entry in a legend has a
> handle - they are the 'children' of the legend handle. After some more
> reading, it seems that there is a handle for each marker, each line and
> each text entry within the legend. Text entries have a 'position'
> property which is a 3-element vector which, from I can gather, is [left
> top ??] - I have no idea what the last element does as changing it appears
> to have no effect.

The documentation for the Position property of text objects indicates that
the position is [x, y, z].

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/text_props.html#Position

If you're viewing the text in the 2D view, changing the Z coordinate would
not have any visible effect. If you created a text object in a 3D axes,
though, it would.


% Note: this clears your figure. Don't use if an important figure is
current:
clf

[xx,yy] = meshgrid(-3:0.1:3, -3:0.1:3);
surf(xx, yy, xx.^2+yy.^2);
h = text(0, 0, 0, 'Hello world!');
P = get(h, 'Position');
for k = [0:0.1:3 -3:0.1:0]
    set(h, 'Position', [P(1:2) k]);
    pause(0.1);
end


> Anyway, adjusting the first position element in the handle of each of my
> category titles has the desired effect. However, the legend is now too
> wide (there is lots of white space) and I have been unable to make it
> narrower - either in script or using the GUI. I think it is due to the
> 'extent' of the position-adjusted text entries but cannot be sure. Anyone
> any ideas?
>
> %%%%%%%%%%%%%
>
> % Code for moving entries:
> l_all = get(l1,'children'); % l1 is the legend handle

Are you aware that you can get the handles to the various objects in the
legend directly from the LEGEND call?

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/legend.html

"[legend_h,object_h,plot_h,text_strings] = legend(...) returns
* legend_h - Handle of the legend axes
* object_h - Handles of the line, patch, and text graphics objects used in
the legend
* plot_h - Handles of the lines and other objects used in the plot
* text_strings - Cell array of the text strings used in the legend"

Anyway, I believe you're right that you'll want to adjust the Extent
property of the legend's text objects you want to move around. You might
also need to be careful about the current setting of the Units property when
you modify Extent and Position. You can read more about all three of those
properties on the first page I linked above.

--
Steve Lord
slord@mathworks.com

Subject: Categories in Plot legend

From: Christopher Hummersone

Date: 13 Oct, 2008 14:34:03

Message: 8 of 8

Thanks a lot for the info Steve. Unfortunately the 'extent' property is read-only. So like I say, adjusting 'x' in the 'position' text property moves the text to where i want it, but still leaves the legend too wide. On closer inspection, I don't think it is the extent, since returning this reveals the same width of the extent rectangle, regardless of the left position:

extent =

    0.3596 0.3750 0.6053 0.1167

extent_new =

    0.0526 0.3750 0.6053 0.1167

Thanks :-)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
legend Christopher Hummersone 6 Oct, 2008 10:45:06
category Christopher Hummersone 6 Oct, 2008 10:45:06
title Christopher Hummersone 6 Oct, 2008 10:45:06
entry Christopher Hummersone 6 Oct, 2008 10:45:06
rssFeed for this Thread

Contact us at files@mathworks.com