# Drawing simple graphs on LaTeX

Let me just say I am NOT drawing graphs from Graph Theory. I am just strictly asking about simple graphs we do in algebra.

I just started with MiKTeX and I am trying to read and absorb everything in the "The Not So Short Introduction to LaTeX" on drawing pictures and graphics.

However I've noticed that to draw even the simplest graph like a rectangle on the first quadrant, you have to be VERY specific with the \put(x, y){\line(x1, y1){length}} command.

Is there a faster way? Yes I realize usually people import graphs from software devices like Maple or Mathematica but I don't have those right now.

Could someone teach me a quick way of drawing them? An example I want to draw is:

a triangle formed by x + y = 1 and the coordinate axis on xy-plane.

My best attempt at this is

\setlength{\unitlength}{2mm}
\begin{picture}(10,20)
\put(0,0){\vector(1,0){40}}
\put(0,0){\vector(0,1){40}}
\put(10,0){\line(-5,6){10}}
\put(10,0){\line(5,-6){10}}
\end{picture}


Unfortunately it displays nothing like I intended.

\put(10,0){\line(-5,6){10}}
\put(10,0){\line(5,-6){10}} does opposite things as I wanted a line straight through.


Could someone also detailedly explain to me what do the parameters (x,y) do after \begin{picture}(x,y)?

• You can quickly create those graphs using Geogebra. It's free to download (unlike Maple and Mathematica) and is available on Mac, Windows, and Linux. It's easy to learn, is well documented, and even lets you export your picture to PSTricks or Tikz code. – DJP Jun 2 '12 at 2:04
• I take it back, I want to do it on MikTeX...it's just tedious to add more programs now lol – Hawk Jun 2 '12 at 2:06

## 1 Answer

I would recommend you use pgfplots for these kind of graphs: If some of these options are not clear, the best way is to comment them out and see the effect on the graph.

## Code:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-1, xmax=2,
ymin=-1, ymax=2,
axis lines=center,
axis on top=true,
domain=0:1,
]

\addplot [mark=none,draw=red,ultra thick] {1-x};
\end{axis}
\end{tikzpicture}
\end{document}


Alternatively you could do this just in tikz: ## Code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [thin, gray, ->] (0,-1) -- (0,2)      % draw y-axis line
node [above, black] {$y$};              % add label for y-axis

\draw [thin, gray, ->] (-1,0) -- (2,0)      % draw x-axis line
node [right, black] {$x$};              % add label for x-axis

\draw [draw=red,ultra thick] (0,1) -- (1,0);% draw the graph

\node [left] at (0,1) {$1$};                % label y-intercept
\node [below] at (1,0) {$1$};               % label x-intercept
\end{tikzpicture}
\end{document}

• Peter, I am really new to this. I've noticed that some packages I have to install, do you know why? Why weren't some of these installed when I first downloaded MikTeX? – Hawk Jun 2 '12 at 3:29
• I think MikTeX has a mode where it installs packages as needed. I personally prefer to download the entire TeXLive distribution and go from there, but you really should look at tikz for basic drawing and pgfplots for graphing. – Peter Grill Jun 2 '12 at 3:37
• Do these packages accumulate a lot of spaces on harddrive? – Hawk Jun 2 '12 at 3:40
• Yes, the do accumulate space, but since hard drives are soooooo cheap a few extra GB should not be an issue. Not that each package takes up that much space since they are all just text files, just saying that it not worth spending time worrying about disk. – Peter Grill Jun 2 '12 at 3:43
• Yes, voting and upvoting are the same thing (up/down arrows near questions/answers), but is seems as if you have only voted three times (see your profile). Selecting and answer is a separate thing, and only the person posting the question can accept an answer. – Peter Grill Jun 2 '12 at 6:08