如何在 ycomb 顶部添加标签?

如何在 ycomb 顶部添加标签?

我使用 tikz 创建了下面的图片。 在此处输入图片描述 但是我不知道如何在使用 ycomb 创建的每条垂直线的顶部添加标签。我正在使用下面的代码。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{figure}
    \centering
%Chart 1, 100% Features
\begin{tikzpicture}[trim axis left]
\begin{axis}[
    scale only axis, % The height and width argument only apply to the actual axis
    height=5cm,
    width=14.90cm,
    grid=both,
    max space between ticks=40, % To control the number of major ticks
    xlabel={Month},
    ylabel={Count},
    xmin=5, xmax=95,
    ymin=0, ymax=100,
    xtick={10,20,30,40,50,60,70,80,90,100},
    xticklabels={\footnotesize \textbf{Jan},\footnotesize \textbf{Feb},\footnotesize \textbf{Mar}, \footnotesize \textbf{Apr}, \footnotesize \textbf{May}, \footnotesize \textbf{June}, \footnotesize \textbf{Jul}, \footnotesize \textbf{Aug},\footnotesize \textbf{Sep}},
    xticklabel shift={.1cm},
    ytick={0,10,20,30,40,50,60,70,80,90,100},
    yticklabels={0\%,10\%,20\%,30\%,40\%,50\%,60\%,70\%,80\%,90\%,100\%},
    legend pos=south east,
    ymajorgrids=true,
    grid style=dashed,
    xtick align=outside
]
 
\addplot[ %A
    color=blue,
    mark=square,
    ]
    coordinates {
(10,83.88)(20,70.36)(30,44.28)(40,30.68)(50,84.30)(60,70.42)(70,44.26)(80,84.50)(90,84.60) };

\addplot[ %B
    color=red,
    mark=square,
    ]
    coordinates {
(10,79.08)(20,17.64)(30,16.28)(40,15.34)(50,78.95)(60,16.16)(70,15.70)(80,78.59)(90,78.19) };

\addplot[ %C  
    color=brown,
    mark=square,
    ]
    coordinates {
(10,85.29)(20,66.21)(30,42.14)(40,30.39)(50,84.98)(60,64.39)(70,39.3)(80,84.02)(90,83.48) };

\addplot[ %D
    color=black,
    mark=square,
    ]
    coordinates {
(10,78.29)(20,60.37)(30,39.79)(40,28.25)(50,77.03)(60,59.41)(70,38.95)(80,76.12)(90,76.03)};

\legend{A,B,C,D}

\addplot+[ycomb,color=purple,dashdotted,thick,no marks] table[x=x,y expr=100] {
x
10
30
57.5
67.5
70
};

\end{axis}
\end{tikzpicture}
\caption{Example Picture}
\end{figure}

\end{document}

有什么方法可以让我在每个垂直虚线的上方添加标签吗?

答案1

\coordinate在环境中添加了 saxis以便使用axis坐标系,然后在axis环境之后添加相对于这些坐标的节点。这样,节点标签就不会被剪裁。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{figure}
    \centering
%Chart 1, 100% Features
\begin{tikzpicture}[trim axis left]
\begin{axis}[
    scale only axis, % The height and width argument only apply to the actual axis
    height=5cm,
    width=14.90cm,
    grid=both,
    max space between ticks=40, % To control the number of major ticks
    xlabel={Month},
    ylabel={Count},
    xmin=5, xmax=95,
    ymin=0, ymax=100,
    xtick={10,20,30,40,50,60,70,80,90,100},
    xticklabels={\footnotesize \textbf{Jan},\footnotesize \textbf{Feb},\footnotesize \textbf{Mar}, \footnotesize \textbf{Apr}, \footnotesize \textbf{May}, \footnotesize \textbf{June}, \footnotesize \textbf{Jul}, \footnotesize \textbf{Aug},\footnotesize \textbf{Sep}},
    xticklabel shift={.1cm},
    ytick={0,10,20,30,40,50,60,70,80,90,100},
    yticklabels={0\%,10\%,20\%,30\%,40\%,50\%,60\%,70\%,80\%,90\%,100\%},
    legend pos=south east,
    ymajorgrids=true,
    grid style=dashed,
    xtick align=outside
]
 
\addplot[ %A
    color=blue,
    mark=square,
    ]
    coordinates {
(10,83.88)(20,70.36)(30,44.28)(40,30.68)(50,84.30)(60,70.42)(70,44.26)(80,84.50)(90,84.60) };

\addplot[ %B
    color=red,
    mark=square,
    ]
    coordinates {
(10,79.08)(20,17.64)(30,16.28)(40,15.34)(50,78.95)(60,16.16)(70,15.70)(80,78.59)(90,78.19) };

\addplot[ %C  
    color=brown,
    mark=square,
    ]
    coordinates {
(10,85.29)(20,66.21)(30,42.14)(40,30.39)(50,84.98)(60,64.39)(70,39.3)(80,84.02)(90,83.48) };

\addplot[ %D
    color=black,
    mark=square,
    ]
    coordinates {
(10,78.29)(20,60.37)(30,39.79)(40,28.25)(50,77.03)(60,59.41)(70,38.95)(80,76.12)(90,76.03)};

\legend{A,B,C,D}

\addplot+[ycomb,color=purple,dashdotted,thick,no marks] table[x=x,y expr=100] {
x
10
30
57.5
67.5
70
};

\coordinate (A1) at (50,99);
\coordinate (A2) at (250,99);
\coordinate (A3) at (530,99);
\coordinate (A4) at (625,99);
\coordinate (A5) at (655,99);
\end{axis}
\node [above=4pt] at (A1) {$A_1$};
\node [above=4pt] at (A2) {$A_2$};
\node [above=4pt] at (A3) {$A_3$};
\node [above=4pt] at (A4) {$A_4$};
\node [above=4pt] at (A5) {$A_5$};
\end{tikzpicture}
\caption{Example Picture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容