summaryrefslogtreecommitdiff
path: root/systems/doc/metapost/source-tutorial/graph.tex
blob: b9d2e0503d4cf68737d4b473b1756bb205fbb1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
\section{Graphing functions}
\label{sec:graphing}

Among the most common types of figures for \TeX{} users are those which
are the graphs of functions of a single variable.  Hobby recognized this
and constructed a package to accomplish this task.  It is invoked by

\begin{lstlisting}[style=MP]
input graph;
\end{lstlisting}

\MP{} has the ability to construct data (i.e., ordered pairs) for
graphing simple functions.  However, for more complicated functions, the
data should probably be constructed using external programs such as
\acro{MATLAB} (or Octave), Maple, Mathematica, Gnuplot, et. al.

A typical data file, say \File{data.d}, to be used with the \File{graph}
package may have contents

\vspace{8pt}
\noindent%
\begin{tabular}[b]{@{\hspace{48pt}}l@{}}
  \lstinputlisting[style=text, boxpos=b]{data.d}
\end{tabular}%
\qquad%
\attach[subject={Graph data}]{data.d}
\vspace{8pt}

This data represents the graph of $f(x)=\sqrt{x}$ for six equally spaced
points in $[0,1]$.  To graph this data, the size of the graph must first
be decided.  Choosing a width of $144\mathrm{\ bp}$ and a height of
$89\mathrm{\ bp}$, a minimally controlled plot (as in
\autoref{fig:data}) of this data can be generated by

\begin{lstlisting}[style=MP]
draw begingraph(144bp,89bp);
   gdraw "data.d";
endgraph;
\end{lstlisting}

The \File{graph} package provides many commands used to customize
generated graphs, and these commands are fully documented in the
manual~\cite{hobby:graph} for the \File{graph} package.

\begin{figure}
  \begin{withattachment}{data.mp}
    \centering
    \includegraphics{data.mps}
  \end{withattachment}
  \caption{$f(x)=\sqrt{x}$ using the \texttt{graph} package}
  \label{fig:data}
\end{figure}