绘制一条直线到图形的峰值点

绘制一条直线到图形的峰值点

对于下面的 MWE:

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

是否可以画一条直线到图表的峰值?它基本上会直观地显示图表的最高点。

答案1

我不确定这是你需要的;我借用了一些代码Jake 的回答pgfplots:标记函数的最大值/最小值自动找到最大值和最小值,然后,我使用了after end axis/.append code以下\draw行:

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

在此处输入图片描述

对于水平线,您可以使用

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

在此处输入图片描述

相关内容