5

I want to label only a maximum value of y coordinate from data of external file.

I tried to use the features of the calc, but I couldn't get it; I wonder if I can use a function maximum.

I've seen a similar solved problem, using psthere, and #1. How can I implement in my code, for pgfplots.

This is the code of my graph, I am using pgfplots and tikz packages:

\begin{figure}[!hbp]
\begin{tikzpicture}
    \begin{axis}[
    xlabel={X},
    ylabel={Y},
    xmax=50,
    xmin=0,
    ymax=.5,
    ymin=-.5,
    width=15cm,
    height=5cm,
    ]
        \addplot[blue,smooth]table{sample.table};
    \end{axis}
\end{tikzpicture}
\caption{Caption}
\end{figure}
Isai
  • 4,153

1 Answers1

3

Here's one option; the mark max, mark min styles receive an optional argument allowing to specify the position of the node with the coordinates for the maximum (the defaults are above for the maximum and below for the minimum):

\documentclass{article}
\usepackage{pgfplots}

\makeatletter
\pgfplotsset{
    compat=1.12,
    /tikz/max node/.style={
        anchor=south,
    },
    /tikz/min node/.style={
        anchor=north,
        name=minimum
    },
    mark min/.style={
        point meta rel=per plot,
        visualization depends on={x \as \xvalue},
        scatter/@pre marker code/.code={%
            \ifx\pgfplotspointmeta\pgfplots@metamin
                \def\markopts{}%
                \coordinate (minimum);
                \node[#1] {(\pgfmathprintnumber[fixed]{\xvalue},\pgfmathprintnumber[fixed]{\pgfplotspointmeta})};%
            \else
                \def\markopts{mark=none}
            \fi
            \expandafter\scope\expandafter[\markopts,every node near coord/.style=green]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter,
    },
    mark min/.default={below},
    mark max/.style={
        point meta rel=per plot,
        visualization depends on={x \as \xvalue},
        scatter/@pre marker code/.code={%
        \ifx\pgfplotspointmeta\pgfplots@metamax
            \def\markopts{}%
               \coordinate (maximum);
                \node[#1] {(\pgfmathprintnumber[fixed]{\xvalue},\pgfmathprintnumber[fixed]{\pgfplotspointmeta})};%
        \else
            \def\markopts{mark=none}
        \fi
            \expandafter\scope\expandafter[\markopts]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter
    },
    mark max/.default={above},
}
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{sample.table}
x y
1 0.2
10 0.1
20 0.4
30 -0.3
40 0.3
50 -0.2
\end{filecontents*}

\begin{document}

\begin{figure}
\begin{tikzpicture}
    \begin{axis}[
    xlabel={X},
    ylabel={Y},
    xmax=50,
    xmin=0,
    ymax=.5,
    ymin=-.5,
    width=15cm,
    height=5cm,
    ]
  \addplot[blue,smooth,mark max=right]table{sample.table};
  \end{axis}
\end{tikzpicture}
\caption{Caption}
\end{figure}

\end{document}

enter image description here

I used a variation of Jake's code in his answer to How can I automatically mark local extrema with pgfplots and scatter?

Gonzalo Medina
  • 505,128
  • Not sure if this should be a complete question, so I ask in a comment for the time being. Swithcing to ymode=log breaks the (x,y) label as it displays (x,log(y)). What do you suggest to circumvent this ? @Jake, what is your opinion on this as original poster ? – BambOo May 10 '19 at 13:39
  • Actually, I cheked this issue further and adding point meta={exp(y)} to the axis options corrects the output, which I find somewhat weird, but fits my need. – BambOo May 10 '19 at 14:48