\documentclass[a4paper,12pt]{article}
\usepackage{array}
\usepackage{xspace}
\usepackage{multirow}
\usepackage{amscd,amsmath,amssymb,amstext,amsxtra,amsbsy,amsgen,amsfonts}
\usepackage{graphics,epsfig,float,subfigure,graphicx}
\usepackage[page]{appendix}
\usepackage{float}
%% sizes and margins
\topmargin -0.44in
\oddsidemargin -4.7mm
\evensidemargin -4.7mm
\textheight 24.6cm%
\textwidth 17.5cm
\def\baselinestretch{1.2} % > 1.0 means more space between the lines
%
\begin{document}
\title{\vspace{-3cm} A few tips on improving 2-D plots in Matlab}
\author{Sandipan Bandyopadhyay\\
Department of Engineering Design\\
Indian Institute Technology Madras\\
Chennai 600 036, India}
\date{}
\maketitle
\input{commands2e}
%\begin{abstract}
%\end{abstract}
%
\section{Introduction}
%
\verb+Matlab+$^\copyright$ is one of the most commonly used software today in the academic community. Perhaps
the ease of data representation is among one of the strongest points in favour of
\verb+Matlab+. There is a host of plotting routines which make it strikingly simple to
present the outcome of one's research. Of these, we shall discuss the most
common one in the following, namely the 2-D XY plots.
Like any good software, there are many different levels at which \verb+Matlab+ can be used
and, as we shall see below, the \verb+plot+ command is a good example of this notion.
It comes with a lot of predefined parameters by default which make the basic plot
most simple: if we have two vectors \bx and \by (of equal length), we can plot \by vs. \bx
with nothing more than \verb+plot(x,y)+.
However, the result of this is not so great, and can be easily improved. The plots
need to be much more than just accurate -- they should be easy to visualise and interpret.
It is easy to forget, particularly for a beginner, that the figures generated by \verb+Matlab+
may have a greater life than their appearance on the screen. For instance,
a plot can be printed (in colour or in grayscale),
or embedded in another document (e.g., \LaTeX\ or Word), or be presented within a slide
(e.g., in PPT or PDF). Therefore some refinements to the basic plot are needed, and
we describe some of these in the following.
%
%
\section{Refining a basic plot}
%
We generate the following data to be used in all the plots:
\begin{verbatim}
x = linspace(0,2*pi); % generate 100 equally spaced points in [0,2*pi]
y1 = sin(x);
y2 = sin(2*x);
\end{verbatim}
The following code piece has the effect of plotting these together in a single
window, with all the \verb+Matlab+ defaults. The result is exported to the Encapsulated
Postscript (EPS) file ``sample\_plot1.eps'' by the last line of the code, and the
same is embedded here as Fig.~\ref{fg:level1}.
%
\begin{verbatim}
% level 1 of improvements
figure(1); % open figure 1
plot(x,y1,x,y2); % the most basic plot
title('Sample plots in Matlab');
xlabel('theta (radians)');
ylabel('sin(theta), sin(2 theta)');
print -deps sample_plot1.eps % export to EPS, editable in Adobe Illustrator
\end{verbatim}
%
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{sample_plot1.eps}
\caption{The default plot generated by Matlab}
\label{fg:level1}
\end{figure}
One can readily identify a few drawbacks of Fig.~\ref{fg:level1}:
\begin{itemize}
\item Both the graphs look the same; there is no information provided for easy
differentiation of the two\footnote{It may be trivial to identify the graphs in this
plot, but that need not be the case always!}.
\item There are no grid lines to help reading of data from the graphs.
\item All the texts (in the title, axes labels) are much smaller compared to the
plot area, and therefore hardly legible.
\item The axes labels should look better in Greek fonts.
\end{itemize}
We try to improve upon our first attempt with the following piece of code:
\begin{verbatim}
% level 2 of improvements
figure(2);
plot(x,y1,'b-', x,y2,'r-.'); % why the dotted line, not just a different colour?
legend('\bf{sin\theta}', '\bf{sin 2\theta}');
title('\bf{Sample plots in Matlab}');
xlabel('\bf{\theta} (radians)');
ylabel('\bf{sin\theta}, \bf{sin 2\theta}');
grid on
print -depsc2 sample_plot2.eps % export to EPS 2, with colours
saveas(gcf,'sample_plot2.fig','fig'); % save as Matlab editable .fig file
\end{verbatim}
The result is shown in Fig. ~\ref{fg:level2}.
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth]{sample_plot2.eps}
\caption{Plot with different colours, grid, bolder text elements, Greek fonts}
\label{fg:level2}
\end{figure}
The improvements are as follows:
\begin{itemize}
\item In the line \verb+plot(x,y1,'b-', x,y2,'r-.')+, we have specified that the
first graph be plotted using a continuous blue line, and the second using a
red ``dash-dot'' pattern. All available colours and patterns can be found in the
documentation of \verb+plot+ available with the commands \verb+help plot+ or
\verb+doc plot+. The difference in patterns allows easy recognition of the
graphs even in grayscale plots.
\item Plot identifiers have been added using the command \verb+legend+.
\item Grid lines have been added using \verb+grid on+.
\item The title and axes labels have been made more prominent using the embedded
\LaTeX\xspace command~\verb+\bf+.
\item Greek fonts have been embedded in the axis labels.
\item The colours have been preserved while saving, by using the \verb+-depsc2+ option
with the print command.
\end{itemize}
%
Still, there are a few problems:
\begin{itemize}
\item The plot lines are not bold enough. This problem becomes very apparent when the
plot is projected onto a bigger screen.
\item The text elements are too small in size.
\end{itemize}
In Fig.~\ref{fg:level3}, we take care of the first problem. First, we obtain
a {\em handle}\footnote{Think of it as something similar to a pointer in C\textbackslash C++.}
to the plots. Using the handle \verb+h+, we can modify any property~(e.g.,
\verb+linewidth+ in our case) of the plots we want\footnote{Use the command ``get(h)'' to see a list of properties and their present values.} . The code
is given below:
\begin{verbatim}
% level 3 of improvements
figure(3);
h = plot(x,y1,'b-', x,y2,'r-.'); % get the 'handle' of the plot
set(h, 'linewidth', 2); % manipulate the plot using the handle
legend('\bf{sin\theta}', '\bf{sin 2\theta}');
title('Sample plots in Matlab', 'fontsize', 15, 'fontweight', 'bold');
xlabel('\bf{\theta} (radians)', 'fontsize', 15);
ylabel('\bf{sin\theta}, \bf{sin 2\theta}', 'fontsize', 15);
grid on;
print -depsc2 sample_plot3.eps % export to EPS, editable in EPS Illustrator
saveas(gcf,'sample_plot3.fig','fig'); % save as Matlab editable .fig file
\end{verbatim}
%
\begin{figure}[hbp]
\centering
\includegraphics[width= 0.6\textwidth]{sample_plot3.eps}
\caption{Plot with thicker lines obtained through Matlab {\em handle graphics}}
\label{fg:level3}
\end{figure}
Finally, in Fig.~\ref{fg:level4}, all the font weights and sizes have been
taken care of in a uniform way. Note the changes between this piece of code and
the previous regarding the treatment of line width and bold fonts. While both
set of commands are fine, the last version is more compact and uniform. Note also
how the axis tic-labels have been made bold by using \verb+gca+, the default handle
to the axes maintained by \verb+Matlab+. Also, the tic-labels on the x-axis have been modified to mark the values in terms of fractions/multiples of $\pi$. The plot has been saved in this case in the
JPEG format also, using the \verb+-djpeg+ option. This format is as popular for embedding
graphics in Word documents as EPS is for \LaTeX.
%
\begin{verbatim}
% level 4 of improvements
figure(4);
plot(x,y1,'b-', x,y2,'r-.', 'linewidth', 2);
set(gca, 'fontsize', 15, 'fontweight','bold'); % gca: get current axis
% change ticks on the x-axis. 0 to 2pi with an interval of pi/4
set(gca,'XTick',0:pi/2:2*pi)
% label ticks in terms of pi
set(gca,'XtickLabel',{'0','pi/2','pi','3pi/2','2pi'});
hl = legend('sin\theta', 'sin 2\theta');
set(hl,'fontweight','bold');
title('Sample plots in Matlab', 'fontsize', 15,'fontweight','bold');
xlabel('\theta (radians)', 'fontsize', 15,'fontweight','bold');
ylabel('sin\theta, sin 2\theta', 'fontsize', 15, 'fontweight','bold');
grid on;
print -depsc2 sample_plot4.eps % export to EPS, editable in EPS Illustrator
print -djpeg sample_plot4.jpg % export to 24-bit JPG format
saveas(gcf,'sample_plot4.fig','fig'); % save as Matlab editable .fig file
\end{verbatim}
%
\begin{figure}[hbp]
\centering
\includegraphics[width= 0.6\textwidth]{sample_plot4.eps}
\caption{Plot with prominent text elements}
\label{fg:level4}
\end{figure}
\newpage
We present below all the figures together to demonstrate the progressive
improvement of the simple plot under consideration. It should be obvious
from this as to which one to use as a template for all our future work!
Doing it this way takes much lesser time than changing the properties via
a GUI. Further, none of the effort is ever lost: the graphs can be regenerated
(and saved, if desired) by simply retaining the piece of code in the same file
where the corresponding data is generated/accessed.
%
\begin{figure}[hbp]
\centering
\label{fg:all}
\mbox{
\subfigure[Matlab defaults]
{\includegraphics[width=0.45\textwidth]{sample_plot1.eps}}
\quad
\subfigure[Different line formats, legends, grid]
{\includegraphics[width=0.45\textwidth]{sample_plot2.eps}}
}
\mbox{
\subfigure[Bold, bigger texts, bold plot lines]
{\includegraphics[width=0.45\textwidth]{sample_plot3.eps}}
\quad
\subfigure[Bold, bigger tic-values on axes]
{\includegraphics[width=0.45\textwidth]{sample_plot4.eps}}
}
\caption{Improvements to a simple Matlab plot}
\end{figure}
\\
The details of each command used here can be obtained through the \verb+Matlab help+ or \verb+doc+
command, e.g., \verb+help gca+. Commands very closely associated with \verb+plot+ are:
\verb+subplot+, \verb+comet+, \verb+plotyy+, \verb+loglog+, \verb+axes+ etc., which
should interest the reader.
\end{document}