7

For the MWE below:

\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}

Is it possible to draw a straight line to the peak of the graph ? It will basically visually indicate the highest point of the the plot.

1 Answer 1

12

I am not sure of this is what you need; I borrowed some code from this answer from Jake to pgfplots: mark max/min value of a function to automatically find the maximum and minimum values, and then, I used after end axis/.append code to \draw the line:

\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}

enter image description here

For a horizontal line you could use

\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}

enter image description here

5
  • @HarishKumar now, in my updated answer, the job is done automatically. Aug 31, 2012 at 2:55
  • Thanks. How about a horizontal line @GonzaloMedina ? Will it be similar to this ?
    – Cemre
    Aug 31, 2012 at 10:45
  • @Cemre yes, it's similar; please see my updated answer. Aug 31, 2012 at 12:08
  • @GonzaloMedina: Nice solution. Already upvoted.
    – user11232
    Aug 31, 2012 at 12:22
  • @HarishKumar thanks you! (the credit is for Jake and the code I borrowed from him). Aug 31, 2012 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .