Skip to content

Tutorial for v1.4

K M Masum Habib edited this page Mar 19, 2015 · 12 revisions

Tutorial


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:

[matlab] function h = setPlotProp(opt, hfig) [/matlab]

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:

[matlab] 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 = 2pift; v = Vmsin(th+phi);

% plot it figure; plot(t*1E3, v); [/matlab]

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:

MQGv1.3: ugly

Now, let us add some spices. Let us set the labels:

[matlab] % change settings opt = []; opt.XLabel = 'Time, t (ms)'; % xlabel opt.YLabel = 'Voltage, V (V)'; % ylabel [/matlab]

If we want to save the plot in a file, we have to assign a file name:

[matlab] % Save? comment the following line if you do not want to save opt.FileName = 'plotSimple1.png'; [/matlab]

Finally, call the setPlotProp function to apply the settings and export it:

[matlab] % apply the settings setPlotProp(opt); [/matlab]

The resulting plot should look like:

MQGv1.3: Simple plot

The full source code for this plot, plotSimple.m, can be found inside the examples folder.

We can change color, linewidth, linestyle etc:

[matlab] opt.Colors = [0, 0, 0]; % [red, green, blue] opt.LineWidth = 2; % line width opt.LineStyle = {'--'}; % line style: '-', ':', '--' etc [/matlab]

MQGv1.3: Line style

See plotLineStyle.m for full source code. We can also change scale, axis limit, tick and grid:

[matlab] 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' [/matlab]

MQGv1.3: Simple log scale

or create a log-log plot:

[matlab] 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' [/matlab]

MQGv1.3: Simple log log

See plotSimpleLog.m and plotSimpleLogLog.m for full source code.

Creating multiple plots in a single set of axes is also easy:

[matlab] % generate the signal t = [0:0.0001:3/f]; th = 2pift; v1 = Vmsin(th); v2 = Vmsin(th - phi); v3 = Vmsin(th - phi*2);

% plot them figure; plot(t1E3, v1); hold on; plot(t1E3, 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); [/matlab]

result: MQGv1.3: Multiple plots

The full source is given in plotMultiple.m. We can change the linestyle, color etc and add a legend:

[matlab] 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 [/matlab]

MQGv1.3: Multiple plots markers

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.

[matlab] opt.BoxDim = [7, 3]; %[width, height] in inches [/matlab]

This code creates a figure with 7in x 3in box.MQGv1.3: SizeSee plotSize.m for more details.

You can also load a previously saved MATLAB fig file and export it using setPlotProp:

[matlab] 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); [/matlab]

Clone this wiki locally