这是我的 MWE:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={$a_1$,$a_2$,$a_3$,$a_4$,$a_5$},
ylabel=Y axis label,
xlabel=X axis label,
ymin=0,
ymax=1,
yticklabels={,,}
]
\addplot+[only marks, error bars/.cd, y dir=both,y explicit]
coordinates {
($a_1$,0.3) +- (0.1,0.1)
($a_2$,0.5) +- (0.1,0.1)
($a_3$,0.4)+- (0.1,0.1)
($a_4$,0.55) +- (0.1,0.1)
($a_5$,0.47)+- (0.1,0.1)};
\coordinate (A) at (axis cs:$a_1$,0.55);
\coordinate (B) at (axis cs:$a_5$,0.55);
\coordinate (O1) at (rel axis cs:0,0);
\coordinate (O2) at (rel axis cs:1,0);
\draw [dashed] (A -| O1) -- (A -| O2);
%\draw [dashed] (B -| O1) -- (B -| O2);
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
可能有更优雅的解决方案。但你可以这样做:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={$a_1$,$a_2$,$a_3$,\textcolor{red}{$a_4$},$a_5$},
ylabel=Y axis label,
xlabel=X axis label,
ymin=0,
ymax=1,
yticklabels={,,}
]
\addplot+[only marks, error bars/.cd, y dir=both,y explicit]
coordinates {
($a_1$,0.3) +- (0.1,0.1)
($a_2$,0.5) +- (0.1,0.1)
($a_3$,0.4)+- (0.1,0.1)
(\textcolor{red}{$a_4$},0.55) +- (0.1,0.1)
($a_5$,0.47)+- (0.1,0.1)};
\coordinate (A) at (axis cs:$a_1$,0.55);
\coordinate (B) at (axis cs:$a_5$,0.55);
\coordinate (O1) at (rel axis cs:0,0);
\coordinate (O2) at (rel axis cs:1,0);
\draw [dashed] (A -| O1) -- (A -| O2);
%\draw [dashed] (B -| O1) -- (B -| O2);
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}