Code covered by the BSD License  

Highlights from
plt

image thumbnail
from plt by Paul Mennen
An alternative to plot and plotyy optimized for data exploration

pub()
% pub.m ------------------------------------------------------------
% Every other plt example uses plotting formats appropriate for data exploration
% which was the main design goal of plt. However plt can also use formats that
% are appropriate for creating plots for publication and this scripts shows how
% to do that by creating three different figures.

% The first is a bar chart displaying the data embedded as comments in
% the code.

% The third (smallest) plot contains two traces with error bars.

% Uses the 'COLORdef' parameter to select a white plot background
% Uses text objects to create specially formatted tick labels
% Uses the 'NoCursor' option to remove the cursor objects
% Uses the 'TraceID','' parameter to remove the TraceID box
% Demonstrates how to embed the plot data inside the script comments
% Demonstrates various ways of modifying the grid lines
% Shows the use of the "+ - < > ." prefixes to modify properties of
%   +    left axis
%   -    right axis
%   <    left y label
%   >    right y label
%   .    x label

% ----- Author: ----- Paul Mennen
% ----- Email:  ----- paul@mennen.org

function pub()
% -----------------------------------------------------------------------------------
%                                    Plot 1
%------------------------------------------------------------------------------------
f = fopen(which(mfilename),'rt');     % read the data from the currently running script
k = 0;                                % count the number of data lines read
start = datenum('01-Jan-93');
while 1
  s = fgetl(f);                       % read next line of the script file
  if ~ischar(s)                          break;    end;
  if length(s)<18 | sum(s(1:2)=='%.')~=2 continue; end;  % process only lines starting with '%.'
  d = findstr(s,'-');    if length(d)~=2 continue; end;  % ignore lines without exactly 2 dashes
  c = findstr(s,',');    if length(c)~=1 continue; end;  % ignore lines without exactly 1 comma
  k = k + 1;
  t(k) = (datenum(s(4:c-1)) - start)/365; % time since start (in years)
  v(k) = sscanf(s(c+1:end),'%f');          % read values (in billions of dollars)
  date{k} = s(d(1)+1:c-1);                 % save only the month and year
end;
fclose(f);

pltpub(vbar(t,950,v),'Position',[8 570 1140 440],'AxisPos',[.5 1.15 1.1 .98],...
   'Figname','Plot 1: Bar chart','Xlim',t([1 end])+[-.2 .2],'Ylim',[450 max(v)*1.01],...
   'LabelX','Years since 1-Jan-93','LabelY','Billions of dollars',...
   'LineWidth',4,'+<.FontSize',14,'<.Color',[0 0 .7]);
p = -1;
for m=1:k text(t(m),500,date(m),'rot',90); end; % create a string for each data element
uicontrol('string','print','pos',[12 6 40 20],...
          'callback','plt(''hcpy'',''init'',gcf);');  % include a print button
yt = get(gca,'ytick');  set(gca,'ytick',yt(2:end));   % remove lowest y tick mark
plt('grid',gca,'update');                             % update grid lines
g = findobj(gcf,'user','grid');  y = get(g,'y');      % get y-values of the grid lines
y(find(y==max(y))) = 0;  set(g,'y',y);                % disable vertical grid lines

% -----------------------------------------------------------------------------------
%                                    Plot 2
%------------------------------------------------------------------------------------
t = (-199:2:199)/200;
b = [-50:50 49:-1:-49];

y = [b.^2;                    b.^3;           filter(ones(1,30),1,1./(b+.5));
     cos(30*t).*abs(t.^-.3);  cos(20*t)./t;   min(20,max(-20,1./cos(30*t.^2)));
     1./(.02+t.^2);           t./(.02+t.^2);  min(15,max(-15,(cos(30*t)./t)));
     humps(t);                humps(-t);      humps(t.^2);
     sin(30*t.^3);            sin(40*t)./t;   sin(40*t).*abs(t.^-.5)];

sz = ones(size(t));
m = min(y')';  y = y - m(:,sz);  % shift functions up so that they are postive
m = max(y')';  y = y ./ m(:,sz); % normalize all functions to one
h = pltpub(t,y,'Figname','Plot 2: Publications with subplots','Position',[8 55 800 480],...
    'SubPlot',[33 33 33 -50 50 50],'SubTrace',3*ones(1,5),'TraceC',('brkbrkbrkbrkbrk')',...
    'Linewidth',repmat({1 2 2},1,5),'AxisPos',[.45 .7 1.1 1.06],'styles','x--x--x--x--x--',...
    'Ylabel',{'functions 1-3' 'functions 4-6' 'functions 7-9' 'functions 10-12' 'functions 13-15'},...
    'Xlabel',{'X axis for functions 1-9' 'X axis for functions 10-15'});

%ax = getappdata(gcf,'axis');
%set(h,{'parent'},{ax(1); ax(1); ax(1);   % put 3 functions in each subplot
%                  ax(2); ax(2); ax(2);
%                  ax(3); ax(3); ax(3);
%                  ax(4); ax(4); ax(4);
%                  ax(5); ax(5); ax(5)});

% -----------------------------------------------------------------------------------
%                                    Plot 3
%------------------------------------------------------------------------------------

time = (0:36)/4;              n = length(time);
t2 = time(2:2:end);           m = length(t2);
temp  = 35 + 15*sin(time/3);  events = 10 + 25*sin(t2/3) + 20*rand(1,m);
tempL =  3 + 7*rand(1,n);     eventsL = 3 + 12*rand(1,m);
tempU =  3 + 7*rand(1,n);     eventsU = 3 + 12*rand(1,m);
t3 = 0:.05:9;                 e3 = interp1(t2,events,t3,'spline');

pltpub('Right',1:2,time,temp,ebar(time,temp,tempL,tempU,60),...
    t3,e3,ebar(t2,events,eventsL,eventsU,50),'LineWidth',{1,3,2,3},...
   '+Ycolor','b','-Ycolor','r','TraceC',('rrbb')',...
   'FigName','Plot 3: Activity & Temperature vs Time of day',...
   'LabelY',{'# of Events' 'Temperature (\circC)'},...
   'LabelX','Time of day (13-Feb-09)','+-<>.FontSize',14,...
   'Xlim',time([1 end])+[-.2 .2],'Ylim',{[0 100] [0 60]},...
   '+XtickLabel',' ','AxisPos',[.7 1.1 1.04 .99],...
   'Position',[825 55 670 480]);

set(gca,'Xtick',0:max(time)); % make sure a vertical grid line appears every hour
plt('grid',gca,'update');

for k=0:9
  text(k,-3.5,sprintf('%2d:00',k+12),'horiz','center','fontw','bold');
end;

% -----------------------------------------------------------------------------------
% This is the data for the bar chart (plot 1) which was exported from excel as a csv:
%------------------------------------------------------------------------------------

%. 16-Feb-93, 1103
%. 10-Jun-93, 1206
%. 05-Nov-93, 1224
%. 31-Mar-94, 1190
%. 31-Jul-94, 1241
%. 11-Oct-94, 1256
%. 05-May-95, 1325
%. 30-Sep-95, 1431
%. 31-Dec-95, 1456
%. 30-Jun-96, 1512
%. 24-Nov-96, 1745
%. 09-May-97, 1829
%. 07-Oct-97, 2355
%. 20-Jan-98, 2192
%. 17-Jul-98, 2307
%. 27-Jan-99, 2147
%. 03-Jun-99, 2465
%. 31-Dec-99, 2468
%. 23-Apr-00, 2461
%. 31-Aug-00, 2534
%. 19-Dec-00, 2462
%. 20-May-01, 2659
%. 19-Jan-02, 2550
%. 11-Oct-02, 2216
%. 14-Apr-03, 2310
%. 18-Sep-03, 2594
%. 19-Jan-04, 2808
%. 13-Sep-04, 2805
%. 18-Jan-05, 3023
%. 11-Apr-05, 3152
%. 25-Sep-05, 3275
%. 23-Dec-05, 3373
%. 15-May-06, 3973
%. 15-Sep-06, 3800
%. 12-Jan-07, 4169
%. 13-Apr-07, 4307
%. 03-Jul-07, 4508
%. 19-Oct-07, 4442
%. 22-Feb-08, 4121
%. 16-May-08, 4430
%. 08-Aug-08, 4192
%. 31-Oct-08, 3298
%. 26-Apr-09, 3218
%. 02-Aug-09, 3553
%. 09-Oct-09, 3787
%. 20-Dec-09, 3790
%. 12-Mar-10, 3901

function out = pltpub(varargin)
  out = plt('ColorDef',0,'TraceID',0,'Options','-All Nocursor',varargin{:});

Contact us at files@mathworks.com