如何在 tikzpicture 上用红色绘制一个 Y 轴值

如何在 tikzpicture 上用红色绘制一个 Y 轴值

我希望红色的 V 值标记水平虚线。如果您需要将 Y 轴标签向左移动,那也没问题。这就是我想要的。

在此处输入图片描述

这是我的 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);
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

答案1

这是一个更简单的方法:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\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,
extra y ticks={0.55},
extra y tick label={\color{red}v},
extra y tick style={grid=major},
major y grid style={dashed,black},
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)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容