# Using tikz or something similar for generating general graphs? [duplicate]

What I'm looking for is graphs that can be generated very easily? for eg just an x-y graph with some linear function or quadratic/cubic/quartic to be plotted? a general case nothing specific without numbered axis etc. I need to be able to distinguish between cases where I have different roots for eg for some general cubic i could have 2 negative and 1 positive etc is there a very easy way to do this? where i can just move different degree polynomials around a graph? I'm no expert using tikz well to be honest i wouldn't know the first thing about it. Also if some generic parabola can be added anywhere is the positive quartile and "slid" up and down anywhere... I don't need anyone to write up code just a push in the right direction would be greatly appreciated.

## marked as duplicate by cmhughes, lockstep, mafp, Guido, Claudio FiandrinoOct 31 '13 at 7:19

With pgfplots:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{graph.tex}
\documentclass[tikz,border=12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\def\f#1{x^2-(#1)*x+3}

\begin{document}
\foreach \n in {-5,-4.5,...,5}{
\begin{tikzpicture}
\useasboundingbox (0,0) rectangle (7,6);
\begin{axis}[
axis x line=center,
axis y line=center,
ymax=6, ymin=-6,
xmax =6, xmin=-6,
xtick=\empty,
ytick=\empty,
enlargelimits=false,
]
{\f{\n}};
\end{axis}
\end{tikzpicture}
}
\end{document}
\end{filecontents*}
%create the graph.pdf.
\immediate\write18{pdflatex graph}
% convert to GIF animation
\immediate\write18{convert -delay 50 -loop 0 -density 200 -alpha remove graph.pdf graph.gif}

\begin{document}
Look for the graph.gif file in the same directory as this file.
\end{document}


For producing gif file imagemagick has to be installed.

• thanks very much that is very helpful just a quick question about the compiling of the file... Instead of an animation i get each graph seperate? Also could you tell me how do i get rid of the coordinates? – LatexNoob Oct 31 '13 at 10:00
• @LatexNoob You have to use imagemagick for converting pdf to gif. See the updated answer. – user11232 Oct 31 '13 at 10:12
• thats perfect thanks a million I actually needed pdf and not gif i was just wondering why wasn't it animating. say if i wanted image number 3 that was produced is there a way to single that out so as to not just copy and paste it into a seperate document? Also is there an easy way to a) shade in an area between roots and b) label the roots say x_1 x_2 x_3 etc? – LatexNoob Oct 31 '13 at 10:28
• @LatexNoob It is \n that controls. Use only one value instead of a loop. – user11232 Oct 31 '13 at 10:31
• The host input file does not need to load tikz and to use standalone class, the simplest one, i.e., article class should be more than enough. What do you think? – kiss my armpit Oct 31 '13 at 13:14

With PSTricks. Compile it with latex-dvips-ps2pdf sequence (faster) or xelatex (slower).

Modify the macro \f to suit your need. For example, the following animation shows how the function behaves for any #1. Enjoy!

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic,plotpoints=1000}

\def\f#1{x^2-(#1)*x+3}

\begin{document}
\multido{\n=-5.0+.5}{21}{%
\begin{pspicture*}(-5,-5)(6,6)
\psaxes{->}(0,0)(-5,-5)(5.5,5.5)[$x$,0][$y$,90]
\psplot[linecolor=blue]{-5}{5}{\f{\n}}
\end{pspicture*}}
\end{document}


## PDF animation

For PDF animation (instead of GIF animation), compile the following with pdflatex -shell-escape host.tex where host.tex is the name of the input file.

% this file is host.tex
% it must be compiled with pdflatex -shell-escape host.tex
% =========================================================
\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{frames.tex}
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic,plotpoints=1000}

\def\f#1{x^2-(#1)*x+3}

\begin{document}
\multido{\n=-5.0+.5}{21}{%
\begin{pspicture*}(-5,-5)(6,6)
\psaxes{->}(0,0)(-5,-5)(5.5,5.5)[$x$,0][$y$,90]
\psplot[linecolor=blue]{-5}{5}{\f{\n}}
\end{pspicture*}}
\end{document}
\end{filecontents*}

\usepackage{pgffor}
\usepackage{animate}

\foreach \compiler/\ext in {latex/tex,dvips/dvi,ps2pdf/ps}
{\immediate\write18{\compiler\space frames.\ext}}

\begin{document}
\animategraphics[controls,autoplay,loop,scale=1]{4}{frames}{}{}
\end{document}


You can use the intuitive control panel below the animation. Is it nice?