# How do I draw graphs?

I'm new to Latex and I think I am going to use it for my physics classes in school. I have found a really good template: https://www.sharelatex.com/templates/journals/aip/ (I use sharelatex) which is great equationwise and such, but I don't seem to be able to draw a graph. I want to draw a distance-time graph but I don't know how to do it!

How would I go on to do that?

## 3 Answers

An example of the height-time graph of an object thrown upward vertically.

\documentclass[pstricks,border=24pt]{standalone}
\usepackage{pst-plot}
\pstVerb
{
/U 20 def
/G -10 def
}
\psset{yunit=.5cm}
\usepackage{siunitx}
\begin{document}
\begin{pspicture}(5,21)
\psaxes[Dy=5](0,0)(4.5,21.5)[$t$ {[\si{\s}]},0][$h$ {[\si{\m}]},90]
\psplot[algebraic,linecolor=blue]{0}{4}{U*x+G/2*x^2}
\end{pspicture}
\end{document}


In this example, the object is thrown at an initial speed 20 meter per second vertically upward under the influence of earth gravity of 10 meter per second squared.

## Just for fun!

Compile with pdflatex -shell-escape animation.tex.

% This file name is animation.tex
\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{frames.tex}
\documentclass[pstricks,border=24pt]{standalone}
\usepackage{pst-plot}
\pstVerb
{
/U 20 def
/G -10 def
}
\def\h(#1){U*#1+G/2*#1^2}
\psset{yunit=.5cm}
\usepackage{siunitx}
\begin{document}
\multido{\n=.0+.4}{11}{%
\begin{pspicture}(5,21)
\psaxes[Dy=5](0,0)(4.5,21.5)[$t$ {[\si{\s}]},0][$h$ {[\si{\m}]},90]
\psplot[algebraic,linecolor=blue]{0}{4}{\h(x)}
\psline[linestyle=dashed,linecolor=gray](\n,0)(*{\n} {\h(x)})(0,0|*{\n} {\h(x)})
\pscircle*[linecolor=red](0,0|*{\n} {\h(x)}){3pt}
\end{pspicture}}
\end{document}
\end{filecontents*}

\usepackage{animate,pgffor}
\foreach \compiler/\ext in {latex/tex,dvips/dvi,ps2pdf/ps}{\immediate\write18{\compiler\space frames.\ext}}
\begin{document}
\animategraphics[controls,autoplay,loop,scale=1]{1}{frames}{}{}
\end{document}


• Note that the trajectory of the moving object is a straight line, not a parabola! That is a common misunderstanding that many students have. – kiss my armpit Oct 26 '13 at 11:13
• "common misunderstanding" ? – kan Oct 26 '13 at 11:28
• @kan: Yes. It is a common misunderstanding for most students. – kiss my armpit Oct 26 '13 at 11:31

Graphics can be drawn with a number of systems, including PGF/TikZ, PSTricks and Asymptote.

Graphs (ie plots) can be drawn with PGFPlots, PST-2dplot and PST-3dplot.

There are most likely other drawing packages that I'm not aware of; have a look around CTAN.

As a followup to ChrisS, I would PGFPlots for this. Here's an example:

\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplots}

\begin{document}

Position vs time graph:

\begin{tikzpicture}
\begin{axis}
\addplot  {5+10*x-5*x^2};
\end{axis}
\end{tikzpicture}

with some styling options:

\begin{tikzpicture}
\begin{axis}[axis lines = middle,smooth,xlabel = $t$ (\si{\s}), ylabel =$y$ (\si{\m}), minor tick num =1, grid=both, no markers]
\addplot +[domain=0:3] {5+10*x-5*x^2};
\end{axis}
\end{tikzpicture}

\end{document}