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

How to change the properties of legend?

Asked by Emerson De Souza on 16 Aug 2013

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;
% DATA GENERATION
Fs = 1000;
time  = 0:1/Fs:1;
Frq = 10;
data =  sin(2*pi*time*Frq);
% CALCULATE AND PLOT PULSEWIDTH
pulsewidth(data,time);figure(gcf);
legend('Location', 'NorthEast');
% PLOT PROPERTIES
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

Emerson

0 Comments

Emerson De Souza

Tags

Products

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

6 Comments

dpb 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

get(hchildren,'type')

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 =
183.0291
182.0291
181.0291
180.0291
179.0291
>> get(hc,'type')    % see what the handles represent
ans = 
  'line'
  'line'
  'line'
  'line'
  'line'
>> get(get(gca,'children'),'type')  % same thing except nested call
ans = 
  'line'
  'line'
  'line'
  'line'
  'line'
>> 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.

Emerson De Souza on 21 Aug 2013

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

dpb 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...

dpb

Contact us