Example: Parameterized plots

Published 2009-09-13 | Author: Yves Delhaye

As a math teacher, I have to explain how parameters affect the graph of a function. By putting the “tikzpicture” inside a LaTeX macro, it is very easy and fast to create multiple graphs by modifying only the parameters and calling the macro.

Download as: [PDF] [TEX]

Parameterized plots

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% under Creative Commons attribution license.
% A work by Yves Delhaye
% Requires GNUPLOT and shell-escape enabled



% Macros for cst. They have to be redefined each time. See inside document
\newcommand{\cA}{1}%	Cste . fct
\newcommand{\cB}{0}%	Cste + fct
\newcommand{\cC}{1}%	Cste . var
\newcommand{\cD}{0}%	Cste + var

%LaTeX Macro for drawing fct with pgf/tikz. Define once, use many!
  \pgfmathparse{0.1+\cA*1.1 +\cB} \pgfmathresult \let\maxY\pgfmathresult% evaluate maxY 
  \pgfmathparse{-0.1-\cA*1.1 +\cB} \pgfmathresult \let\minY\pgfmathresult% evaluate minY
     \pgfmathparse{\maxY < 1} \pgfmathresult \let\BmaxY\pgfmathresult% What if maxY < 1? Then set Boolean to 1
       \pgfmathparse{1.2} \pgfmathresult \let\maxY\pgfmathresult% Correct maxY to have correct graph
     \pgfmathparse{\minY > 0} \pgfmathresult \let\BminY\pgfmathresult% What if minY > 0? Then set Boolean to 1
       \pgfmathparse{0} \pgfmathresult \let\minY\pgfmathresult% Correct minY to have correct graph
%        DRAW the graph of the function from here on
   \draw[very thin,color=gray] (-0.1,\minY) grid (7.9,\maxY);% GRID use minY & maxY
    \draw[->] (-0.2,0) -- (8.2,0) node[right] {$x$};
    \draw[->] (0,\minY) -- (0,\maxY) node[above] {$f(x)$};% y axis use minY & maxY too
    \draw[smooth,samples=200,color=blue] plot function{(\cA)* (cos((\cC)*x+(\cD))) + \cB} 
        node[right] {$f(x) = \cA{} . cos(\cC{} . x + \cD{}) + \cB{}$};
% units for cartesian reference frame
    \foreach \x in {0,1}
        \draw (\x cm,1pt) -- (\x cm,-3pt)
            node[anchor=north,xshift=-0.15cm] {$\x$};
    \foreach \y/\ytext in {1}
        \draw (1pt,\y cm) -- (-3pt,\y cm) node[anchor=east] {$\ytext$};
% END of macro

% And now use it!
% Change the parameters
% WITHOUT rewriting the code for the picture    
% And do it again    
% And again    
% And again  

% % This is where the test on maxY is useful.
%     \renewcommand{\cA}{3}
%     \renewcommand{\cB}{-14}
%     \renewcommand{\cC}{2}
%     \renewcommand{\cD}{-2}
%     \FctAss{}
% % And here the test on minY is useful.
%     \renewcommand{\cA}{3}
%     \renewcommand{\cB}{14}
%     \renewcommand{\cC}{2}
%     \renewcommand{\cD}{-2}
%     \FctAss{}


  • #1 David Olivieri, September 24, 2009 at 11:21 p.m.

    Great Site!! Thanks for the help. This example is exactly what I was looking for...


  • #2 Dominik, October 19, 2009 at 12:18 a.m.

    You could improve the \FctAss command by parameterize it itself:

    \newcommand{\FctAss}[4]{ ... }

    This allows you to use #1-#4 inside the command definition and \FctAss{1}{0}{1}{0} for the typesetting.

    Alway performing a \renewcommand before the use of \FctAss isn't intuituve ;-)


  • #3 ben, February 3, 2010 at 2:25 p.m.

    Hey everyone and thanks !! you guys are helping me pretty much on a daily basis...

    anyway, I'd like to plot a function that shows an hysteresis, and putting arrow to show what side of the curve means x going up or down... Scilab does that pretty easily, but I couldn't find a way to do it conveniently with tikz.

    thanks for your help !

  • #4 Michel AlSharidah, February 3, 2012 at 2:52 a.m.


    This is a great example but i could not get the function plot on the graphs, the only thing that shows is the axis, grid, and the text but no plot. am i missing something?

    I just noticed the below, pls help


    Preview: Fontsize 10pt Preview: PDFoutput 1

    Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 59.

    Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 67.

    Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 75.

    Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 83.

    Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 91.

  • #5 Thiago, October 14, 2012 at 2:54 p.m.

    Michael, you have to use gnuplot to produce the graphs. If you use Linux, install it and put the code --shell-escape on the line of the command pdflatex on your editor.


  • #6 Haidong, February 19, 2013 at 11:10 a.m.

    Hi, I have the same problem as Micheal's. I've already installed gnuplot, but my operation system is Windows. What can I do to get those commands work? Thank you!

  • #7 Vladimir, March 31, 2013 at 3:05 p.m.

    Haidong, use --enable-write18 option to pdtlatex

Adding comments is currently not enabled.