2

I would like to be able to specify a function and then graph that with with pgfplot only, or use gnuplot to compute the coordinates. As far as I know there are two main differences in how the functions are parsed:

  1. pgf uses ^ and gnuplot uses ** for exponentiation. This is easily fixed with the translate gnuplot=true, so is not a problem.

  2. Another is that pgf uses degrees for trigonometric functions and gnuplot uses radians. Is there a way to adjust this also?

But, ignoring the possible problem with #2 above, is there a way to specify the function once and use pgfplots to graph it with or without gnuplot? So far, I have been specifying the pgfplots function as follows:

\pgfmathdeclarefunction{FunctionF}{1}{\pgfmathparse{(#1)^(3)}}%

But, when I want to gnuplot I have been defining it as follows:

\newcommand*{\FunctionFGnuplot}{(x)^3}%

Is there not a way I can specify the function once and use it both cases? Just to be clear I don't have an issue with the fact that the format of the addplot command is different between the two, just the specification of what the function is.

Here is a MWE illustrating this:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\newcommand*{\XAxisMin}{-1.0}
\newcommand*{\XAxisMax}{3.0}
\newcommand*{\YAxisMin}{-2.0}
\newcommand*{\YAxisMax}{10}

\newcommand*{\DomainMinF}{\XAxisMin}
\newcommand*{\DomainMaxF}{2.2}

\pgfkeys{/pgfplots/Axis Style/.style={
    clip=false,% so that we can see labels placed outside
    xmin=\XAxisMin, xmax=\XAxisMax,
    ymin=\YAxisMin, ymax=\YAxisMax, 
    domain=\DomainMinF:\DomainMaxF,
    width=6.5cm
}}

% Gnuplot options here  have no effect if not using GnuPlot
\pgfkeys{/pgfplots/Plot Style/.style={
    translate gnuplot=true,% can use ‘^’ instead of ‘**’
    id=foo,
    mark=none,%
    domain=\DomainMinF:\DomainMaxF,%
    samples=50,%
    ultra thick,
}}

\newcommand*{\AddLabel}[1]{\node [align = center] at (axis cs: 0.4,5) {#1};}%
%-----------------------------

% I would like to only have to specify the function here once
\pgfmathdeclarefunction{FunctionF}{1}{\pgfmathparse{(#1)^(3)}}%
\newcommand*{\FunctionFGnuplot}{(x)^3}%


\begin{document}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, blue]
        ({x},{FunctionF(x)});%
    \AddLabel{1. without \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
    \addplot [Plot Style, red]
        gnuplot% Use Gnuplot for Graph 2
        {\FunctionFGnuplot};%
    \AddLabel{2. with \\ Gnuplot}
\end{axis}
\end{tikzpicture}
\end{document}
  • 1
    At least in this case, you can say either \addplot {\FunctionFGnuplot}; or \addplot gnuplot {\FunctionFGnuplot}, both will give you the same result. Can you give an example where this does not work? – Jake Oct 26 '11 at 11:26
  • 1
    I would agree with Jake's comment: the approach to define a macro which contains the math expression should work in most cases. There are most certainly cases where this will fail (like boolean arithmetics). But the most important case where it fails is (as already noted as point 2): trigonometric functions. Perhaps it would be a good idea to introduce some sort of switch for tikz/pgfplots to use radians (at least inside of plot coordinates). – Christian Feuersänger Oct 29 '11 at 18:39
  • As of Nov 3, 2011, the \addplot {<function>} method also supports '==, !=, <=, >=, ?`. See tex.stackexchange.com/questions/33584/… – Christian Feuersänger Nov 3 '11 at 21:26
2

An answer to question 2: You can tell gnuplot to use degrees instead of radians by issuing a set angles degrees; in the gnuplot code.

You can define a new style to add that line into the gnuplot code generated by PGFplots:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\makeatletter
\pgfplotsset{
     use degrees in gnuplot/.code={
        \xdef\pgfplots@gnuplot@format{\pgfplots@gnuplot@format set angles degrees;}
    }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:360, samples=50, enlarge x limits=false, clip=false]
\addplot [red!50, line width=0.25cm]
    {sin(x)}
    node [anchor=north west] {PGFplots};
\addplot [gray] 
    gnuplot {sin(x)}
    node [anchor=west] {Standard Gnuplot};
\addplot [use degrees in gnuplot,black, ultra thick]
    gnuplot {sin(x)}
    node [anchor=south west] {Gnuplot with degrees};
\end{axis}
\end{tikzpicture}
\end{document}

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.