Trazar una línea recta hasta el punto máximo de un gráfico

Trazar una línea recta hasta el punto máximo de un gráfico

Para el MWE a continuación:

\documentclass{report}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols,automata}

\begin{document}

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[%
width=1*\textwidth,
height=8cm,
xlabel={Query},
ylabel={Elapsed Time (in seconds)},
grid = both,
minor x tick num=3,
minor y tick num=3,
enlarge x limits=0
]
\addplot [
color=red,
solid,
line width=1.0pt
]
coordinates{

(0,6.66003) (1,9.10814) (2,182.761) (3,109.154) (4,246.253) (5,206.129) (6,212.653) (7,240.088) (8,4.49266) (9,263.248) (10,257.65) (11,147.81) (12,87.5402) (13,127.389) (14,167.028) (15,2.35324) (16,120.594) (17,2.4295) (18,13.0306) (19,126.756) (20,64.0324) (21,100.611) (22,14.7892) (23,2.41528) (24,132.193) (25,9.94301) (26,156.253) (27,123.016) (28,186.208) (29,102.549) (30,194.67) (31,184.039) 
};

\end{axis}
\end{tikzpicture}
\caption{Blah}
\end{figure}

\end{document}

¿Es posible trazar una línea recta hasta el pico del gráfico? Básicamente indicará visualmente el punto más alto de la trama.

Respuesta1

No estoy seguro de que esto sea lo que necesitas; Tomé prestado un código deesta respuesta de Jakeapgfplots: marca el valor máximo/mínimo de una funciónpara encontrar automáticamente los valores máximo y mínimo, y luego, usé after end axis/.append codela \drawlínea:

\documentclass{report}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\colorlet{maxcolor}{blue}

\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols,automata}

\makeatletter
\pgfplotsset{
    /tikz/max node/.style={color=blue,
        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 [min node] {
                    (\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 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 [max node] {
                (\pgfmathprintnumber[fixed]{\xvalue},%
                \pgfmathprintnumber[fixed]{\pgfplotspointmeta})
            };
        \else
            \def\markopts{mark=none}
        \fi
            \expandafter\scope\expandafter[\markopts]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter
    }
}
\makeatother


\begin{document}

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[%
width=1*\textwidth,
height=8cm,
xlabel={Query},
ylabel={Elapsed Time (in seconds)},
grid = both,
minor x tick num=3,
minor y tick num=3,
enlarge x limits=0,
after end axis/.append code={
  \draw [thick,maxcolor] (maximum) --({rel axis cs:0,0}-|maximum);
},
mark options={fill=maxcolor,draw=maxcolor}
]
\addplot [mark max,
color=red,
solid,
line width=1.0pt
]
coordinates{

(0,6.66003) (1,9.10814) (2,182.761) (3,109.154) (4,246.253) (5,206.129) (6,212.653) (7,240.088) (8,4.49266) (9,263.248) (10,257.65) (11,147.81) (12,87.5402) (13,127.389) (14,167.028) (15,2.35324) (16,120.594) (17,2.4295) (18,13.0306) (19,126.756) (20,64.0324) (21,100.611) (22,14.7892) (23,2.41528) (24,132.193) (25,9.94301) (26,156.253) (27,123.016) (28,186.208) (29,102.549) (30,194.67) (31,184.039) 
};
\end{axis}
\end{tikzpicture}
\caption{Blah}
\end{figure}

\end{document}

ingrese la descripción de la imagen aquí

Para una línea horizontal puedes usar

\documentclass{report}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\colorlet{maxcolor}{blue}

\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols,automata}

\makeatletter
\pgfplotsset{
    /tikz/max node/.style={color=blue,
        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 [min node] {
                    (\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 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 [max node] {
                (\pgfmathprintnumber[fixed]{\xvalue},%
                \pgfmathprintnumber[fixed]{\pgfplotspointmeta})
            };
        \else
            \def\markopts{mark=none}
        \fi
            \expandafter\scope\expandafter[\markopts]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter
    }
}
\makeatother


\begin{document}

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[%
width=1*\textwidth,
height=8cm,
xlabel={Query},
ylabel={Elapsed Time (in seconds)},
grid = both,
minor x tick num=3,
minor y tick num=3,
enlarge x limits=0,
after end axis/.append code={
  \draw [thick,maxcolor] ({rel axis cs:0,1}|-maximum) -- ({rel axis cs:1,1}|-maximum);
},
mark options={fill=maxcolor,draw=maxcolor}
]
\addplot [mark max,
color=red,
solid,
line width=1.0pt
]
coordinates{

(0,6.66003) (1,9.10814) (2,182.761) (3,109.154) (4,246.253) (5,206.129) (6,212.653) (7,240.088) (8,4.49266) (9,263.248) (10,257.65) (11,147.81) (12,87.5402) (13,127.389) (14,167.028) (15,2.35324) (16,120.594) (17,2.4295) (18,13.0306) (19,126.756) (20,64.0324) (21,100.611) (22,14.7892) (23,2.41528) (24,132.193) (25,9.94301) (26,156.253) (27,123.016) (28,186.208) (29,102.549) (30,194.67) (31,184.039) 
};
\end{axis}
\end{tikzpicture}
\caption{Blah}
\end{figure}

\end{document}

ingrese la descripción de la imagen aquí

información relacionada