MATLAB Answers


How to change the properties of legend?

Asked by Emerson De Souza on 16 Aug 2013
Accepted Answer by dpb

INTRO: Hi, I use the command pulsewidth(data,time) to display the width of peaks as described in the example below:

clear all; close all;
Fs = 1000;
time  = 0:1/Fs:1;
Frq = 10;
data =  sin(2*pi*time*Frq);
legend('Location', 'NorthEast');
box on;
set(gca,'FontSize',18, 'FontName','Arial', 'FontWeight','bold','Color','w','LineWidth',3);
xlabel('TIME','FontSize',18, 'FontName','Arial','FontWeight','bold');
ylabel('AMPLITUDE','FontSize',18, 'FontName','Arial','FontWeight','bold');
scrsz = get(0,'ScreenSize'); set(gcf,'Position',scrsz,'PaperPositionMode','auto');

QUESTION: How can I change the thickness of the lines and the sizes of the symbols displayed in the legend?

This legend is generated automatic with the command pulsewidth and I don't have access to the data. The only thing I could do until now was to fix the location of the legend.

I hope someone knows how to help me.

Thanks in advance





1 Answer

Answer by dpb
on 16 Aug 2013
 Accepted answer

From the doc for legend --

LEGH = legend returns the handle to legend on the current axes or empty if none exists.

So, after your call to pulsewidth, include

lh=legend; % return the legend handle

then you can do whatever you want w/ the properties thereof


on 16 Aug 2013

You need to read up on handle graphics and enhancing plots with set and get

The 10 numbers are the 'handles' of the objects that are the children of the axes object -- iow, the lines, patches, etc., etc., etc., ... or perhaps the higher level group in some instances.

After you have those handles, then you can ascertain which is what by


which will give a list of what the various handles are...

If I do something really trivial here...

>> plot(rand(3,5))      % a simple plot of 5 lines
>> hc=get(gca,'children') % get the children handles
hc =
>> get(hc,'type')    % see what the handles represent
ans = 
>> get(get(gca,'children'),'type')  % same thing except nested call
ans = 
>> get(hc(1))   % see what properties are for the first child...
         DisplayName: ''
          Annotation: [1x1 hg.Annotation]
               Color: [1 0 0]
           LineStyle: '-'
           LineWidth: 0.5000
              Marker: 'none'
          MarkerSize: 6
     MarkerEdgeColor: 'auto'
     MarkerFaceColor: 'none'
               XData: [1 2 3]
               YData: [0.4756 0.3625 0.7881]
               ZData: [1x0 double]
        BeingDeleted: 'off'
       ButtonDownFcn: []
            Children: [0x1 double]
            Clipping: 'on'
           CreateFcn: []
           DeleteFcn: []
          BusyAction: 'queue'
    HandleVisibility: 'on'
             HitTest: 'on'
       Interruptible: 'on'
            Selected: 'off'
  SelectionHighlight: 'on'
                 Tag: ''
                Type: 'line'
       UIContextMenu: []
            UserData: []
             Visible: 'on'
              Parent: 173.0291
           XDataMode: 'auto'
         XDataSource: ''
         YDataSource: ''
         ZDataSource: ''

What you do is use those handles to set() the desired properties as wanted. As said, you can use the figure editor to explore as well and to see how changing something affects the plot appearance interactively.

Thank you, I read your suggestions and tried different things until it worked.

on 21 Aug 2013

That's the way to get somewhere w/ handle graphics--seems daunting 'cuz there's so much folderol to deal with with the large number of objects and properties and there's no way to find in documentation except by a very deep hunt keeping on drilling down so all one can do is dig in and start...

But, once you do it a time or two you get the hang of the general idea and then it's just "handle diving" to find what it is they called whatever the property is that does the effect desired for the most part...

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!