2

i would like to plot a function with PGF and also plot some points that belong to a function with a label. I defined the function and plotted it with this code:

\begin{tikzpicture} [declare function = {f(\x) = -0.05*\x^3 -0.5*\x^2;}]
\begin{axis}
\addplot [blue, domain=-10:10] {f(x)};

\end{axis}
\end{tikzpicture}

And i tried to mark the points with labels with \node, but i always get errors when i try to do so letting PGF calculate the y-value using the declared function f(\x). How can i solve my problem?

1
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Rmano Feb 18 at 18:25
3

Next time, please post a complete example --- a lot depends on your preamble. Anyway, I think that you are looking for this:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta,positioning,calc}
\begin{document}
\begin{tikzpicture} [declare function = {f(\x) = -0.05*\x^3 -0.5*\x^2;}]
\begin{axis}
\addplot [blue, domain=-10:10] {f(x)};
\path (-1,{f(-1)}) node[circle, fill=red]{}
    node[below=4pt ]{-1, \pgfmathparse{f(-1)}\pgfmathresult};
\end{axis}
\end{tikzpicture}
\end{document}

In coordinates, you have to hide the () to the parser, so you have to protect them in a couple of braces.

enter image description here

1
  • Thank you! Solved my problem! – Andrew Feb 19 at 12:44
2

You weren't very specific so I present only two solutions to your problem. The advantage over Rmano's answer is, that it works for any \addplot command, i.e. not only for the ones that plot a given function.

For details, please have a look at the comments in the code.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[
    declare function={
        f(\x) = -0.05*\x^3 - 0.5*\x^2;
    },
]
    \begin{axis}
        % first plot the function
        \addplot+ [
            domain=-10:10,
            % here we only want the line
            no markers,
            % in case the next plot should use the same color
            % (from the `cycle list`)
            forget plot,
        ] {f(x)};

        % second set the marks at the desired positions
        \addplot+ [
            % here we only want the markers ...
            only marks,
            % ... at the positions in this list ...
            samples at={-9,-5},
            % ... together with their corresponding y value
            % (which is the default) ...
            nodes near coords,
        ] {f(x)};

        \addplot+ [
            only marks,
            samples at={-1,3},
            % ... but you can define it your needs, e.g.
            nodes near coords={%
                \footnotesize
                $(\pgfmathprintnumber{\pgfkeysvalueof{/data point/x}},
                  \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}})$%
            },
            % but you dan do a lot more stuff.
            % If you are curious, search in the PGFPlots manual for
            % "nodes near coords" or here on TeX.SX for the tag "nodes near coords"
            % (https://tex.stackexchange.com/questions/tagged/nodes-near-coords)
        ] {f(x)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.