如何在 pgfplots 中绘制并标记从数据点到 x 轴的垂直线

如何在 pgfplots 中绘制并标记从数据点到 x 轴的垂直线

我有下图,其中仅包含四个不同的值$x$。每个$x$值都对应一个特定的数据集,我需要在图中对其进行标记。

因此,我想从该图的顶部到底部绘制一条垂直线,并在轴上为其添加标签(字符串而不是数字)x。我该怎么做?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]
            \begin{tikzpicture}
                \begin{axis}[
                    xlabel=$x$,
                    ylabel=Running Time (sec),
                    xmode=log,
                    every axis/.append style={font=\tiny},
                    ]
                \addplot[color=green,mark=triangle*,mark size=2] coordinates {
                    (735323,3.477)
                    (875713,8.598)
                    (986324,6.697)
                    (4847571,54.948)
                };
                \addplot[color=blue,mark=square*,mark size=2,mark options={solid}] coordinates {
                    (735323,2.759941)
                    (875713,3.898159)
                    (986324,3.172807)
                    (4847571,72.299393)
                };
                \addplot[color=purple,mark=*,mark size=2,mark options={solid}] coordinates {
                    (735323,1.607309)
                    (875713,4.377542)
                    (986324,4.460953)
                    (4847571,34.504368)
                };
                \end{axis}
            \end{tikzpicture}
\end{figure}
\end{document}

答案1

这是实现此目的的一种方法。

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]
            \begin{tikzpicture}
                \begin{axis}[
                    ymin=-10,clip=false,
                    xlabel=$x$,
                    ylabel=Running Time (sec),
                    xmode=log,
                    every axis/.append style={font=\tiny},
                    extra x ticks={735323,875713,986324,4847571},
                    extra x tick labels={$L_1$,$L_2$,$L_3$,$L_4$},
                    extra x tick style={x tick label style={yshift=-2ex,rotate=90,anchor=east},grid=major,major grid style={draw=blue, thick}}
                    ]
                \addplot[color=green,mark=triangle*,mark size=2] coordinates {
                    (735323,3.477)
                    (875713,8.598)
                    (986324,6.697)
                    (4847571,54.948)
                };
                \addplot[color=blue,mark=square*,mark size=2,mark options={solid}] coordinates {
                    (735323,2.759941)
                    (875713,3.898159)
                    (986324,3.172807)
                    (4847571,72.299393)
                };
                \addplot[color=purple,mark=*,mark size=2,mark options={solid}] coordinates {
                    (735323,1.607309)
                    (875713,4.377542)
                    (986324,4.460953)
                    (4847571,34.504368)
                };
                %\draw[blue] (axis cs: 4847571,72.299393) -- (axis cs: 4847571,-10)node[anchor=east,rotate=90]{Some label};
                \end{axis}
            \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

你也可以自己画出来。

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]
            \begin{tikzpicture}
                \begin{axis}[
                    ymin=-10,clip=false,
                    xlabel=$x$,
                    ylabel=Running Time (sec),
                    xmode=log,
                    every axis/.append style={font=\tiny},                    
                    ]
                \addplot[color=green,mark=triangle*,mark size=2] coordinates {
                    (735323,3.477)
                    (875713,8.598)
                    (986324,6.697)
                    (4847571,54.948)
                };
                \addplot[color=blue,mark=square*,mark size=2,mark options={solid}] coordinates {
                    (735323,2.759941)
                    (875713,3.898159)
                    (986324,3.172807)
                    (4847571,72.299393)
                };
                \addplot[color=purple,mark=*,mark size=2,mark options={solid}] coordinates {
                    (735323,1.607309)
                    (875713,4.377542)
                    (986324,4.460953)
                    (4847571,34.504368)
                };
                \draw[blue] (axis cs: 4847571,\pgfkeysvalueof{/pgfplots/ymin}) -- 
                      (axis cs: 4847571,\pgfkeysvalueof{/pgfplots/ymax})node[anchor=west,rotate=90]{Some label};
                \draw[blue] (axis cs: 986324,\pgfkeysvalueof{/pgfplots/ymin}) -- 
                      (axis cs: 986324,\pgfkeysvalueof{/pgfplots/ymax})node[anchor=west,rotate=90]{Some};
                \draw[blue] (axis cs: 875713,\pgfkeysvalueof{/pgfplots/ymin}) --
                      (axis cs: 875713,\pgfkeysvalueof{/pgfplots/ymax})node[anchor=west,rotate=90]{Some label};
                \draw[blue] (axis cs: 735323,\pgfkeysvalueof{/pgfplots/ymin}) --
                      (axis cs: 735323,\pgfkeysvalueof{/pgfplots/ymax})node[anchor=west,rotate=90]{Some};
                \end{axis}
            \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容