-
Notifications
You must be signed in to change notification settings - Fork 91
Tutorial for v1.4
To create a beautiful figure using PlotPub, all you need to know is how to use the set-plot-properties (setPlotProp
) function. It has the following syntax:
function h = setPlotProp(opt, hfig)
where, opt
is the options structure that controls the figure properties and hfig
is figure handle.
Let us walk you through an example. Assume that we have data for 3 cycle of a 50 Hz AC voltage signal:
clear all;
%% lets plot 3 cycles of 50Hz AC voltage
f = 50; % frequency
Vm = 10; % peak
phi = 0; % phase
% generate the signal
t = [0:0.0001:3/f];
th = 2*pi*f*t;
v = Vm*sin(th+phi);
% plot it
figure;
plot(t*1E3, v);
where, f
is the frequency, Vm
is the peak voltage, t
is time and v
is the AC voltage signal. Result? An utterly ugly looking figure punching at your face:
Now, let us add some spices. Let us set the labels:
% change settings
opt = [];
opt.XLabel = 'Time, t (ms)'; % xlabel
opt.YLabel = 'Voltage, V (V)'; % ylabel
opt.Title = 'Voltage as a function of time'; % title
If we want to save the plot in a file, we have to assign a file name:
% Save? comment the following line if you do not want to save
opt.FileName = 'plotSimple1.png';
Finally, call the setPlotProp
function to apply the settings and export it:
% apply the settings
setPlotProp(opt);
The resulting plot should look like:
The full source code for this plot, plotSimple.m
, can be found inside the examples
folder.
We can change color, linewidth, linestyle etc:
opt.Colors = [0, 0, 0]; % [red, green, blue]
opt.LineWidth = 2; % line width
opt.LineStyle = {'--'}; % line style: '-', ':', '--' etc
See plotLineStyle.m
for full source code. We can also change scale, axis limit, tick and grid:
opt.YScale = 'log'; % 'linear' or 'log'
opt.YLim = [1E-3, 1E3]; % [min, max]
opt.YTick = [1E-3, 1E-1, 1E1, 1E3]; %[tick1, tick2, .. ]
opt.YGrid = 'on'; % 'on' or 'off'
or create a log-log plot:
opt.YScale = 'log'; % 'linear' or 'log'
opt.XScale = 'log'; % 'linear' or 'log'
opt.YLim = [1E-3, 1E3]; % [min, max]
opt.YTick = [1E-3, 1E-1, 1E1, 1E3]; %[tick1, tick2, .. ]
opt.YGrid = 'on'; % 'on' or 'off'
opt.XGrid = 'on'; % 'on' or 'off'
See plotSimpleLog.m
and plotSimpleLogLog.m
for full source code.
Creating multiple plots in a single set of axes is also easy:
% generate the signal
t = [0:0.0001:3/f];
th = 2*pi*f*t;
v1 = Vm*sin(th);
v2 = Vm*sin(th - phi);
v3 = Vm*sin(th - phi*2);
% plot them
figure;
plot(t*1E3, v1);
hold on;
plot(t*1E3, v2);
plot(t*1E3, v3);
hold off;
% change settings
opt = [];
opt.XLabel = 'Time, t (ms)'; % xlabel
opt.YLabel = 'Voltage, V (V)'; % ylabel
opt.YTick = [-10, 0, 10]; % [tick1, tick2, .. ]
opt.YLim = [-11, 11]; % [min, max]
% Save? comment the following line if you do not want to save
opt.FileName = 'plotMultiple.png';
% apply the settings
setPlotProp(opt);
result:
The full source is given in plotMultiple.m
. We can change the linestyle, color etc and add a legend:
opt.XLim = [0, 80]; % [min, max]
opt.Colors = [ % three colors for three data set
1, 0, 0; % data set 1
0.25, 0.25, 0.25; % data set 2
0, 0, 1; % data set 3
];
opt.LineWidth = [2, 2, 2]; % three line widths
opt.LineStyle = {'-', '-', '-'}; % three line styles
opt.Markers = {'o', '', 's'};
opt.MarkerSpacing = [15, 15, 15];
opt.Legend = {'\theta = 0^o', '\theta = 45^o', '\theta = 90^o'}; % legends
Here, opt.Colors(1,:)
, opt.LineWidth(1)
and opt.LineStyle{1}
set properties of data set 1 and so on. The full source is given in plotMarkers.m
.
By default, plotPub
creates figures with 6in x 2.5in box size. You can easily change the figure size using the following code.
opt.BoxDim = [7, 3]; %[width, height] in inches
This code creates a figure with 7in x 3in box.
See plotSize.m
for more details.
You can also load a previously saved MATLAB fig file and export it using setPlotProp
:
clear all;
% load previously generated fig file
figFile = 'single.fig';
open(figFile)
% change settings
opt.XLabel = 'Time, t (ms)'; % xlabel
opt.YLabel = 'Voltage, V (V)'; %ylabel
opt.BoxDim = [6, 5]; %[width, height]
% Save? comment the following line if you do not want to save
opt.FileName = 'plotSize.png';
% apply
setPlotProp(opt);