14

I would like to plot a linear function as y=40-0.2x and an hyperbola as y=5/x. How could I make it? I've tried, a code from this document in p. 160, but the curve wasn't in the same page with the axes.

EDIT: Having used the Tikz method mentionned below, I would like to make a graph for y=5/(x-1)+1.

The result as it is shown in Microsoft Mathematics is: enter image description here

However, using LaTeX, I don't have the same result. My code is:

\documentclass[tikz,border=3mm]{standalone}
\usepackage[english,greek]{babel}
\usepackage{ucs} 
\usepackage[utf8x]{inputenc}
\begin{document}
\begin{tikzpicture}[xscale=0.08,yscale=0.09,domain=0.140:60,samples=800]
    \draw[->] (0,0) -- (65,0) node[below] {$x$};
    \draw[->] (0,0) -- (0,35) node[left] {$y$};
    \foreach \i in {10,20,...,50} {
        \draw (\i,1) -- (\i,-1) node[below] {$\i$};
    }
    \foreach \i in {5,10,...,30} {
        \draw (1,\i) -- (-1,\i) node[left] {$\i$};
    }
    \draw[green] plot (\x,{5/(\x-1)+1});
\end{tikzpicture}
\end{document}

Where is my fault?

1
  • 1
    You could have a look at pgfplots, there are plenty of examples in the manual and here on the site. pgfplots is built on TikZ, but is specifically for creating plots if functions or data. Also, note that the manual you link to is for a very old version of pgf/TikZ, see CTAN or texdoc.net for a more recent version. Jun 30, 2013 at 7:52

3 Answers 3

32
+400

Remarks

Keep in mind for both methods, that 5/x has a singularity at 0. At this point the function value will be infinity, which is kind of hard to draw for PGF and will therefore throw an error:

  • ! Package PGF Math Error: You've asked me to divide `5' by `0', but I cannot divide any number by `0' (in '{5/0}').

  • ? Dimensions too large

Method 1: Using TikZ

The advantage of using TikZ here is, that you can easily place nodes on the plot. The disadvantage is, that you have to scale x and y dimension, because else the drawing will be 220cm wide.

Implementation

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[xscale=0.04,yscale=0.08,domain=0.125:220,samples=400]
    \draw[->] (-10,0) -- (225,0) node[below] {$x$};
    \draw[->] (0,-5) -- (0,45) node[left] {$y$};
    \foreach \i in {50,100,...,200} {
        \draw (\i,1) -- (\i,-1) node[below] {$\i$};
    }
    \foreach \i in {10,20,...,40} {
        \draw (1,\i) -- (-1,\i) node[left] {$\i$};
    }
    \draw[blue] plot (\x,{40-0.2*\x});
    \draw[red] plot (\x,{5/\x});
\end{tikzpicture}
\end{document}

Output

tikz

Method 2: Using PGFPlots

This is much more elegant and the code is much shorter.

Implementation

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[domain=0.125:220,samples=400]
        \addplot+[mark=none] {40-0.2*x};
        \addplot+[mark=none] {5/x};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        domain=0.125:220,
        xmin=-10, xmax=220,
        ymin=-5, ymax=45,
        samples=400,
        axis y line=center,
        axis x line=middle,
    ]
        \addplot+[mark=none] {40-0.2*x};
        \addplot+[mark=none] {5/x};
    \end{axis}
\end{tikzpicture}
\end{document}

Output

pgfplots1 pgfplots2

Method 3: PSTricks (Just for fun)

Using the package pst-plot you get access to advanced plotting features. Also PSTricks is much faster than TikZ for plotting, because it makes use of the Postscript language.

Implementation

Compile with xelatex or latex -> dvips -> ps2pdf.

\documentclass[pstricks,border=3mm]{standalone}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}[xAxisLabel=$x$,yAxisLabel=$y$](-0.5,0)(0.5,6.5)
\begin{psgraph}[arrows=->,Dx=50,Dy=10](0,0)(-10,-5)(220,45){8cm}{6cm}
    \psplot[plotpoints=200,linecolor=blue]{0}{220}{40 0.2 x mul sub}
    \psplot[plotpoints=200,linecolor=red]{0.125}{220}{5 x div}
\end{psgraph}
\end{pspicture}

\begin{pspicture}[xAxisLabel=$x$,yAxisLabel=$y$,xAxisLabelPos={c,-12},yAxisLabelPos={-35,c}](-1,-1)(0.5,6.5)
\begin{psgraph}[axesstyle=frame,xticksize=-5 45,yticksize=-10 220,Dx=50,Dy=10](0,0)(-10,-5)(220,45){8cm}{6cm}
    \psplot[plotpoints=200,linecolor=blue]{0}{220}{40 0.2 x mul sub}
    \psplot[plotpoints=200,linecolor=red]{0.125}{220}{5 x div}
\end{psgraph}
\end{pspicture}
\end{document}

ps1 ps2

4
  • Except for my edit in question, I have just another one: when we use PGFPlots can we have only the x-axis and the y-axis, and NOT a whole box?
    – Y_gr
    Jun 30, 2013 at 9:37
  • 1
    @giannis: I adjusted my answer. Jun 30, 2013 at 9:50
  • You could just do axis lines=center. (For reference, @giannis, see the manual section 4.8.9 Axis lines.) Jun 30, 2013 at 9:51
  • @TorbjørnT. You are right. I adapted this in my follow up answer. Jun 30, 2013 at 10:02
9

Answer to the edit

You need to adjust the plot limits by hand. There is no way around that, because no plotting software in the world can know, what limits you want.

Implementation

\documentclass[tikz,border=3mm]{standalone}
\usepackage[english,greek]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[xscale=1.5,yscale=0.04,domain=1.037:4,samples=800]
    \draw[->] (0,0) -- (4.5,0) node[below] {$x$};
    \draw[->] (0,0) -- (0,135) node[left] {$y$};
    \foreach \i in {1,2,...,4} {
        \draw (\i,1) -- (\i,-1) node[below] {$\i$};
    }
    \foreach \i in {20,40,...,130} {
        \draw (0.05,\i) -- (-0.05,\i) node[left] {$\i$};
    }

    \draw[green] plot (\x,{5/(\x-1)+1});
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        domain=1.037:4,
        xmin=0, xmax=4.5,
        ymin=0, ymax=135,
        samples=800,
        axis lines=center,
    ]
        \addplot+[mark=none,color=green] {5/(x-1)+1};
    \end{axis}
\end{tikzpicture}
\end{document}

Output

tikz pgfplots

3

Just for fun with PSTricks.

\documentclass[preview,border=12mm,varwidth]{standalone}
\usepackage{pst-plot}

\psset{plotpoints=200,linewidth=1pt,algebraic,xAxisLabel=$x$,yAxisLabel=$y$}

\def\f{40-0.2*x}
\def\g{5/x}

\begin{document}

\begin{psgraph}[arrows=->,Dx=50,Dy=10](0,0)(-10,-5)(220,45){8cm}{6cm}
    \psplot[linecolor=blue]{0}{220}{\f}
    \psplot[linecolor=red]{0.125}{220}{\g}
\end{psgraph}

\vspace{1cm}
\psset{xAxisLabelPos={c,-12},yAxisLabelPos={-35,c}}
\begin{psgraph}[axesstyle=frame,xticksize=-5 45,yticksize=-10 220,Dx=50,Dy=10](0,0)(-10,-5)(220,45){8cm}{6cm}
    \psplot[linecolor=blue]{0}{220}{\f}
    \psplot[linecolor=red]{0.125}{220}{\g}
\end{psgraph}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.