%\documentclass[final,harvardcite]{ltugboat}
\documentclass[final]{ltugboat}
\PrelimDraftfalse
\InputIfFileExists{tugboat.dates}%
  {}{}
\InputIfFileExists{tb72pages.tex}%
  {}{\def\TBpstplot{1001}}
\setcounter{page}{\TBpstplot}

\overfullrule=6pt

\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[scaled=0.96]{helvet}
\usepackage{mathptmx}
\usepackage{courier}
\usepackage{comment}
\usepackage{tabularx}

\usepackage{graphicx}
%\usepackage{pstcol}
%\usepackage{pst-plot}%
%
\newcommand\verbI[1]{{\small\texttt{#1}}}
\newcommand\CMD[1]{{\small\textbackslash\texttt{#1}}}
\setlength{\belowcaptionskip}{\abovecaptionskip}
%
\usepackage{listings}
\lstset{language=[LaTeX]TEX,basicstyle=\ttfamily\footnotesize,%
        extendedchars=true,numbers=left,numberstyle=\tiny,%
        xleftmargin=2em,breaklines=true}
%
\usepackage{ifpdf}
\ifpdf%
        \usepackage[%
                pdftex,%
                pdfauthor={Jana and Herbert Voss},%
                pdftitle={2D Plots},%
                linktocpage,%
                colorlinks]{hyperref}
\hypersetup{%
colorlinks,linkcolor={black}, citecolor={black}, pagecolor={black}, 
urlcolor={black}
}

\else
        \usepackage[%
                ps2pdf,%
                pdfauthor={Jana and Herbert Voss},%
                pdftitle={2D Plots},%
                linktocpage,%
                colorlinks]{hyperref}
\fi
\makeatletter
\usepackage{url}

\begin{document}

\sectitle{Graphics}


\title{The plot functions of \texttt{pst-plot}\thanks{Translated from
``Die Plot-Funktionen von pst-plot,'' 
\textsl{Die TeXnische Kom\"odie} \textbf{2/02}, June 2002, pages 27-34,
with permission.}}

\author{Jana Voß}
\address{Wasgenstr. 21\\14129 Berlin \textsc{GERMANY}}
\netaddress{Jana@perce.de}
%
\author{Herbert Voß}
\address{Wasgenstr. 21\\14129 Berlin \textsc{GERMANY}}
\personalURL{http://www.perce.de}
\netaddress{voss@perce.de}

\maketitle

\begin{abstract}
  Plotting of external data records is one of the standard problems of
  technical and industrial publications. One common approach is
  importing the data files into an external program, such as
  \verbI{gnuplot}, provided with axes of coordinates and further
  references, and finally exported to \LaTeX. By contrast, in this
  article we explain ways to get proper data plotting without using
  external applications.
\end{abstract}

\section{Introduction}

The history and the meaning of \PS{} have been covered sufficiently in
many articles. For the programming language \PS{} have a look at
\cite{PostScript,ThinkPostScript}.  The package \verbI{pst-plot}
\cite{pst-plot} under consideration here is part of the \verbI{pstricks}
project. It must be loaded into a \LaTeX{} document as usual with 
\CMD{usepackage\{pst-plot\}} or alternatively, for documents written in Plain\TeX,
with \CMD{input pst-plot.tex}.

\verbI{pst-plot} provides three plot macros for the representation of
external data, with the following syntax:

{\small\begin{verbatim}
\listplot*[<parameter>]{<data macro>}
\dataplot*[<parameter>]{<data macro>}
\fileplot*[<parameter>]{<file name>}
\end{verbatim}}
    
    The starred forms have the same meaning as with all macros of
    \verbI{pstricks}: to plot the data in a reversed mode.  Thus, for a
    default black-on-white diagram one produces the negative with the
    starred command form, namely white-on-black. Additionally, a negative plot
    implies that \PS{} closes the path of the points from the last to
    the first one and fills all points inside this closed path with the
    actual fillcolor. For our purposes in this article there will be no
    real sense in this negative view; therefore, all of the following
    examples are plotted with the normal (unstarred) form only.

For further information about the package \verbI{pstricks} have a look at the (more
or less) official documentation \cite{pstricks}, or the extensive
description in the ``standard \LaTeX'' book \cite{companion} or in
\cite{girou:01:,girou:02:}. Altogether, however, these do not fully
describe the substantial differences between these three plot macros.

For all of the examples in this article, the complete \verbI{pspicture}
environment is indicated, so that the examples may be directly copied.
The Documentation of the \verbI{multido} macro used here can be found in the
package itself \cite{multido}; macros not otherwise mentioned are
described in the \verbI{pstricks} documentation \cite{pstricks}.

The general \verbI{plotstyle} parameter is particularly important, and
can take the values shown in table \ref{tab:options}.

\begin{table}
\caption{Possible options}\label{tab:options}
\begin{tabularx}{1.0\linewidth}{r|X}
style option& meaning\\
\hline
\verbI{plotstyle=dots} & plot $(x,y)$ as a dot\\
\verbI{=line} & draw a line from a dot to the following one\\
\verbI{=polygon} & nearly the same as \verbI{line}, but with a line from the last to the first dot\\
\verbI{=curve} & interpolation between three dots, whereby the curve can go beyond the point of origin and/or termination point\\
\verbI{=ecurve} & like \verbI{curve}, but ends at the first/last dot\\
\verbI{=ccurve} & like \verbI{curve}, but closed\\
\end{tabularx}
\end{table}

By default, the \verbI{line} option is selected.

The following general commands are also useful in conjunction with the
plot commands. They are also defined by the \verbI{pst-plot} package:

{\small\begin{verbatim}
\readdata{<data macro name>}{<file name>}
\savedata{<data macro name>}{<file name>}
\end{verbatim}}
    
    In the following examples only the \CMD{readdata} macro is used,
    but it would be straightforward to create examples with
    \CMD{savedata}.

\section{Examples for \CMD{listplot}}

The syntax of \CMD{listplot} is :

{\small\begin{verbatim}
\listplot{<data macro name>}
\end{verbatim}}
    
    The data macro may contain any additional \AllTeX- or
    \PS-commands. The \AllTeX{} macros are expanded first before they
    are passed as a \textbf{list} of $x|y$ values to \PS. The data
    records can be defined inside the document like

{\small\begin{verbatim}
\newcommand{\dataA}{
 1.00000000  1.00000000
 0.56000000  0.31000000
 0.85841600  0.17360000
 ...
\end{verbatim}}

\noindent or can be read from an external data file with the \CMD{readdata} macro:
 
{\small\begin{verbatim} \readdata{\dataA}{/anyPath/data.dat}
\end{verbatim}}
    
    The example in figure \ref{fig:listplot1} shows the Henon
    attractor, a typical graphic of a system with chaotic
    behavior\cite{voss:chaos}.
    
    Figure \ref{fig:listplot2} is nearly the same as
    figure~\ref{fig:listplot1}, with the addition of \PS{} code to
    get
    the "Draft" watermark. (Some familiarity with \PS{} is needed to
    fully understand its operation.) To save space, listing
    \ref{lst:listplot2} does not contain the data, which is nothing
    more than a sequence of pairs of floats, each value separated by a
    space, as shown above.

\begin{figure}[htb]
\centering
\begin{comment}
\PSforPDF{%
\readdata{\henon}{henon.dat}
\psset{xunit=1.5cm, yunit=3cm}
\begin{pspicture}(-2,-0.5)(2.25,1.25)
  \psaxes{->}(0,0)(-2,-0.5)(1.5,1.25)
  \listplot[%
    showpoints=true,%
    plotstyle=curve]{\henon}
\end{pspicture}%
}
\end{comment}
\includegraphics{fig4}
\caption{Example for \CMD{listplot}}\label{fig:listplot1}
\end{figure}

\begin{figure}[htb]
\centering
\begin{comment}
\newcommand{\dataA}{
 1.00000000  1.00000000
 0.56000000  0.31000000
 0.85841600  0.17360000
 0.11249564  0.26610896
 1.24788537  0.03487365
-1.20752013  0.38684447
-0.71282656 -0.37433124
-0.10602649 -0.22097623
 0.76283584 -0.03286821
 0.12916912  0.23647911
 1.21245320  0.04004243
-1.07681914  0.37586049
-0.29387634 -0.33381393
 0.54182291 -0.09110166
 0.48615456  0.16796510
 0.82762650  0.15070791
 0.16435741  0.25656422
 1.21766498  0.05095080
-1.08414872  0.37747614
-0.31506881 -0.33608610
 0.52096746 -0.09767133
 0.51150245  0.16149991
 0.78474587  0.15856576
 0.27177620  0.24327122
 1.13690951  0.08425062
-0.77704042  0.35244195
 0.48298173 -0.24088253
 0.42320673  0.14972434
 0.89181467  0.13119409
-0.01408602  0.27646255
 1.27617683 -0.00436667
-1.34958997  0.39561482
-1.22719125 -0.41837289
-1.58701052 -0.38042929
  gsave
  /Helvetica findfont 40 scalefont setfont
  45 rotate
  0.9 setgray
  -60 10 moveto (DRAFT) show
  grestore
}
\psset{xunit=1.5cm, yunit=3cm}
\begin{pspicture}(-2,-0.5)(2.25,1.25)
  \psaxes{->}(0,0)(-2,-0.5)(1.5,1.25)
  \listplot[%
    showpoints=true,%
    linecolor=red,%
    plotstyle=curve]{\dataA}
\end{pspicture}%
\end{comment}
\includegraphics{fig5}
\caption{Example for modified \CMD{listplot}}\label{fig:listplot2}
\end{figure}


\begin{lstlisting}[caption={\LaTeX\ source for figure \ref{fig:listplot1}},%
        label={lst:listplot1}]
\readdata{\henon}{henon.dat}
\psset{xunit=1.5cm, yunit=3cm}
\begin{pspicture}(-3,-0.5)(2.25,1.25)
  \psaxes{->}(0,0)(-2,-0.5)(1.5,1.25)
  \listplot[%
    showpoints=true,%
    linecolor=red,%
    plotstyle=curve]{\dataA}
\end{pspicture}
\end{lstlisting}


\begin{lstlisting}[caption={\LaTeX\ source for figure \ref{fig:listplot2}},%
        label={lst:listplot2}]
\newcommand{\DataA}{%
  [ ... data ... ]
  gsave          % save graphic status
  /Helvetica findfont 40 scalefont setfont
  45 rotate      % rotate by 45 degrees
  0.9 setgray    % 1 is color white
  -60 10 moveto (DRAFT) show
  grestore
}
\psset{xunit=1.5cm, yunit=3cm}
\begin{pspicture}(-3,-0.5)(2.25,1.25)
  \psaxes{->}(0,0)(-2,-0.5)(1.5,1.25)
  \listplot[%
    showpoints=true,%
    plotstyle=curve]{\dataA}
\end{pspicture}
\end{lstlisting}


Naturally, \verbI{[... data ...]} is replaced by all
the $x|y$-values; they're omitted here only to save space.

As an alternative to direct modification of the data set passed to 
\CMD{listplot}, one can redefine the macro \verbI{defScalePoints} from
\verbI{pst-plot}. For example, to change the $x|y$ values and then
rotate the whole plotted graphic (don't ask why!), the redefinition is
as shown in listing \ref{lst:listplot3}.

\begin{lstlisting}[%
        caption={\LaTeX\ source for figure \ref{fig:listplot3}},%
        label={lst:listplot3}]
\makeatletter
\pst@def{ScalePoints}<%
%-------------------------------------
  45 rotate % rotate the whole object
%-------------------------------------
  /y ED /x ED
  counttomark dup dup cvi eq not { exch pop } if
  /m exch def /n m 2 div cvi def
  n {
%-------------------------------------
     exch % exchange the last two elements
%-------------------------------------
     y mul m 1 roll
     x mul m 1 roll
     /m m 2 sub
     def } repeat>
\makeatother
\end{lstlisting}

\noindent This gives figure \ref{fig:listplot3}.



\begin{figure}[htb]
\centering
\begin{comment}
\makeatletter
\pst@def{ScalePoints}<%
%-----------------------------------------------
  45 rotate % rotate the whole object
%-----------------------------------------------
  /y ED /x ED
  counttomark dup dup cvi eq not { exch pop } if
  /m exch def /n m 2 div cvi def
  n {
%-----------------------------------------------
     exch % exchange the last two elements
%-----------------------------------------------
     y mul m 1 roll
     x mul m 1 roll
     /m m 2 sub
     def } repeat>
\makeatother
\readdata{\henon}{henon.dat}
\begin{pspicture}(-0.5,-2.75)(3,3)
\psset{xunit=3cm, yunit=1.5cm}
  \psaxes{->}(0,0)(-0.5,-2)(1.25,2)
  \listplot[%
    showpoints=true,%
    plotstyle=curve]{\henon}
\end{pspicture}%
\end{comment}
\includegraphics{fig6}
\caption{Example for \CMD{listplot} with a redefined \verbI{ScalePoints}}\label{fig:listplot3}
\end{figure}

Thus, the advantage of \CMD{listplot} is that one can easily modify the
data values without any external program. Here is one more example---suppose
you have the following data records:

{\small\begin{verbatim}
1050, 0.368
1100, 0.371
1200, 0.471 
1250, 0.428    
1300, 0.391    
1350, 0.456    
1400, 0.499    
1500, 1.712    
1550, 0.475    
1600, 0.497
\end{verbatim}}

\noindent which perhaps came automatically from a technical device. The
unit of the x-values is micrometer but it makes more sense to use
millimeter for the plot. A redefinition of \verbI{ScalePoints} makes it
very easy to plot the data with this change of scale:

\begin{lstlisting}[caption={Rescale all x values},label={lst:lstplot}]
  \makeatletter
\pst@def{ScalePoints}<%
  /y ED /x ED
  counttomark dup dup cvi eq not { exch pop } if
  /m exch def /n m 2 div cvi def
  n {
     y mul m 1 roll
     x mul 1000 div m 1 roll% <-- divide by 1000
     /m m 2 sub
     def } repeat>
\makeatother
\end{lstlisting}

\begin{figure}[htb]
\centering
\begin{comment}
\makeatletter
\begin{pdfpic}
\pst@def{ScalePoints}<%
  /y ED /x ED
  counttomark dup dup cvi eq not { exch pop } if
  /m exch def /n m 2 div cvi def
  n {
     y mul m 1 roll
     x mul 1000 div m 1 roll% <-- divide by 1000
     /m m 2 sub
     def } repeat>
\makeatother
\def\dataA{%
1050 0.368
1100 0.371
1200 0.471 
1250 0.428    
1300 0.391    
1350 0.456    
1400 0.499    
1500 1.712    
1550 0.475    
1600 0.497 }%
\psset{xunit=5}
\begin{pspicture}(1,-0.25)(2,2.25)
        \psaxes[Dx=0.25,Ox=1]{->}(1,0)(2,2)
        \psaxes[Dx=0.05,Ox=1,labels=none,ticksize=0.05](1,0)(2,0)
        \listplot[plotstyle=dots,linecolor=red]{\dataA}
\end{pspicture}%
\end{pdfpic}
\end{comment}
\includegraphics{fig7}
\caption{Example for modified data values with a redefined \verbI{ScalePoints}} \label{fig:lstplot4}
\end{figure}



\section{Examples for \CMD{dataplot}}

\CMD{dataplot} has the same syntax as \CMD{listplot}, so the first
question is, what is the difference between the two? \CMD{listplot}
builds a list of all the data and then multiplies all values with the
length unit.  This takes some time, so you may prefer a so-called
``quick plot'', where the data can be passed more quickly to \PS,
depending on the plotstyle and especially the option \verbI{showpoints}.
Table \ref{tab:quickplot} shows whether this is possible. A quick plot
is not possible with \CMD{listplot}, whereas \CMD{dataplot} uses it
whenever possible.  When it is not possible, \CMD{dataplot} simply calls
\CMD{listplot}.


\def\hI{\hangindent=3em}
\begin{table}[htb]
\caption{Possible options for a ''quick plot``}\label{tab:quickplot}
\begin{tabularx}{\columnwidth}{lX|l}
plotstyle & options & macro\\\hline
\verbI{line} & all, except & quick plot\\
        & \verbI{linearc}, \verbI{showpoints}, \verbI{arrows},  & \CMD{listplot}\\
\verbI{polygon} & all, except & quick plot\\
        & \verbI{linearc}, \verbI{showpoints}  & \CMD{listplot}\\
\verbI{dots} & all & quick plot\\
\verbI{bezier} & all, except & quick plot\\
        &\verbI{arrows}, \verbI{showpoints} & \CMD{listplot}\\
\hI\verbI{cbezier} & all, except& \CMD{listplot}\\
        &\verbI{showpoints} & quick plot\\
\verbI{curve} & all & \CMD{listplot}\\
\verbI{ecurve} & all & \CMD{listplot}\\
\verbI{ccurve} & all & \CMD{listplot}
\end{tabularx}
\end{table}


\CMD{dataplot} needs to be passed a macro holding the data.  The data is
typically saved in an external file, which can be read by (for instance)
the \CMD{readdata} macro, as follows:

{\small\begin{verbatim}
\readdata{<object name>}{<data file>}
\end{verbatim}}

\noindent For example: 

{\small\begin{verbatim}
\readdata{\feigenbaum}{feigenbaum.data}
\end{verbatim}}
    
The amount of data is limited only by \TeX's memory. The above example
can be plotted with:

{\small\begin{verbatim}
\dataplot{\feigenbaum}
\end{verbatim}}
    
    Overlays with different data files are also possible.  For example,
    figure~\ref{fig:dataplot} shows the use of two different data files
    which are plotted using one coordinate system. It shows the sorting
    time for ``Bubble-Sort'' and ``Select-Sort{}'' as a function of the
    number of the elements.

\begin{figure}
\centering
\begin{comment}%
\psset{xunit=0.0005cm,yunit=0.005cm}
\begin{pspicture}(0,-50)(10000,1100)
  \readdata{\bubble}{bubble.data}
  \readdata{\select}{select.data}
  \dataplot[%
        plotstyle=line,%
        linecolor=blue]{\bubble}
  \dataplot[%
        plotstyle=line,%
        linecolor=red,%
        linewidth=2pt]{\select}
  \psline{->}(0,0)(10000,0)
  \psline{->}(0,0)(0,1000)
  \rput[l](40,920){time}
  \rput[r](9990,-30){elements}
  \rput[l](4500,800){Bubble-Sort}
  \rput[l](7500,200){Select-Sort}
\end{pspicture}%
\end{comment}
\includegraphics{fig0}
\caption{Example for \CMD{dataplot}}\label{fig:dataplot}
\end{figure}

\begin{lstlisting}[caption={\LaTeX\ source for figure \ref{fig:dataplot}}]
\psset{xunit=0.0005cm,yunit=0.005cm}
\begin{pspicture}(0,-50)(10000,1100)
  \readdata{\bubble}{bubble.data}
  \readdata{\select}{select.data}
  \dataplot[%
        plotstyle=line,%
        linecolor=blue]{\bubble}
  \dataplot[%
        plotstyle=line,%
        linecolor=red,%
        linewidth=2pt]{\select}
  \psline{->}(0,0)(10000,0)
  \psline{->}(0,0)(0,1000)
  \rput[l](20,995){time}
  \rput[r](9990,-20){elements}
  \rput[l](4500,800){Bubble-Sort}
  \rput[l](7500,200){Select-Sort}
\end{pspicture}
\end{lstlisting}

In summary, the advantage of \CMD{dataplot} is the possibility of a
''quick plot``, and the advantage of \CMD{listplot} is that it is easy
to manipulate the data values before they are plotted.


\section{Examples for \CMD{fileplot}}

\CMD{fileplot} can be used whenever $(x|y)$ data that is saved in a file
is to be plotted.  The values must be given as pure numerical values in
pairs, one pair on each line, and may have spaces, commas, parentheses,
and braces as punctuation, as follows:

\begin{verbatim}
x y
x,y
(x,y)
{x,y}
\end{verbatim}

The tab character (\CMD{t} or \acro{ASCII} \CMD{009}) is often used as a
separator, but tab is \emph{not} valid here. Tabs may be converted to
spaces in many ways, for example with the standard Unix utility
\texttt{tr}:

{\small\begin{verbatim}
tr '\t' ' ' <inFile >outFile
\end{verbatim}}
    
The data files may also contain \% characters, but no other characters
are allowed.
 
Our first example for \CMD{fileplot} is shown in
figure~\ref{fig:fileplot1}, which is an \acro{UV/VIS} absorber spectrum
$A=\lg \frac{I_{0}}{I}$ as a function of the wavelength. The second
example (figure~\ref{fig:fileplot2}) shows the evolution of a population
as a function of the spawn factor (Feigenbaum diagram
\cite{voss:chaos}). The source code for these images is shown in
listings~\ref{lst:fileplot1} and~\ref{lst:fileplot2}.

\begin{figure}[htb]
\centering
\begin{comment}%
\psset{xunit=0.0025cm,yunit=1.1cm}
\begin{pspicture}(-25,-.25)(1950,4)
  \fileplot[plotstyle=line, linewidth=1.5pt]{fileplot.data}
  \psaxes[dx=400,Dx=400]{->}(1900,4)
  \multido{\n=200+200}{9}{%
    \psline[linestyle=dotted](\n,0)(\n,4)
  }
  \multido{\n=+1}{5}{%
    \psline[linestyle=dotted](0,\n)(1800,\n)%
  }
\end{pspicture}%
\end{comment}
\includegraphics{fig1}
\caption{Example for \CMD{fileplot}}\label{fig:fileplot1}
\end{figure}

\begin{lstlisting}[caption={\LaTeX\ source for figure \ref{fig:fileplot1}},label={lst:fileplot1}]
\psset{xunit=0.0025cm,yunit=1.1cm}
\begin{pspicture}(-25,-.25)(1950,4)
  \fileplot[plotstyle=line]{fileplot.data}
  \psaxes[dx=400,Dx=400]{->}(1900,4)
  \multido{\n=200+200}{9}{%
    \psline[linestyle=dotted](\n,0)(\n,4)%
  }
  \multido{\n=+1}{5}{%
    \psline[linestyle=dotted]%
       (0,\n)(1800,\n)%
  }
\end{pspicture}
\end{lstlisting}


\begin{figure}[htb]
\centering
\begin{comment}%
\psset{xunit=1.25cm,yunit=5cm}
\begin{pspicture}(-0.25,-0.05)(4.25,1.2)
  \fileplot[plotstyle=dots]{feigenbaum.data}
  \psaxes{->}(0,0)(4.05,1)
  \rput(4,-0.05){$x$}
  \rput(0.2,1.05){$y$}
  \rput[l](0.2,3.75){Feigenbaum-Diagram}
\end{pspicture}%
\end{comment}
\includegraphics{fig2}
\caption{Another example for \CMD{fileplot}}\label{fig:fileplot2}
\end{figure}


\begin{lstlisting}[caption={\LaTeX\ source for figure \ref{fig:fileplot2}},label={lst:fileplot2}]
\psset{xunit=1.5cm,yunit=6cm}
\begin{pspicture}(-0.25,-0.05)(4.25,1)
  \fileplot[plotstyle=dots]{%
     feigenbaum.data}
  \psaxes{->}(0,0)(4.05,1)
\end{pspicture}
\end{lstlisting}

As you may see in listing \ref{lst:fileplot}, \CMD{fileplot} does little
of its own.  It first calls \CMD{readdata} to read the data, and then,
depending on the kind of data and specified options, \CMD{fileplot} uses
\CMD{dataplot} for a quick plot if possible.  Otherwise, it falls back
to \CMD{listplot}.

\begin{lstlisting}[caption={The source of the \CMD{fileplot} macro},label={lst:fileplot}]
\def\fileplot{\def\pst@par{}\pst@object{fileplot}}
\def\fileplot@i#1{%
  \pst@killglue
  \begingroup
    \use@par
    \@pstfalse
    \@nameuse{testqp@\psplotstyle}%
    \if@pst
      \dataplot@ii{\pst@readfile{#1}}%
    \else
      \listplot@ii{\pst@altreadfile{#1}}%
    \fi
  \endgroup
  \ignorespaces%
}
\end{lstlisting}

\CMD{fileplot} has the advantage of being easy to use, but the
disadvantage of needing a lot of memory: \TeX{} has to read the all the
data values before it can process anything. As a rule of thumb, when
there are more than 1000 data entries \TeX{}'s main menory must be
increased. Furthermore, the running time may be enormous, especially on
slow machines.

To prevent such problems, one can use the \CMD{PSTtoEPS} macro to create
an \texttt{eps} file. For more information, see the documentation of
\verbI{pstricks} \cite{pstricks}.

\bibliographystyle{plain}
\bibliography{\jobname}
%\bibliographystyle{ltugbib}

\makesignature

\end{document}
