考虑以下代码:
\documentclass[oneside,article]{memoir}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, positioning, arrows.meta, decorations.pathreplacing}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.misc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[baseline=(current bounding box.center), scale=0.85]
\begin{axis}[ symbolic x coords={$p$[\texttt{A}]=0.137, $p$[\texttt{N}]=0.488},
xtick=data,
axis x line*=bottom,
axis y line=left,
enlarge x limits=0.2,
ytick = {0.15, 0.30, 0.45},
yticklabels={0.15,0.30,0.45},
ylabel=$p$-value,
x=3.5 cm,
ymin = 0,
ymax = 0.55,
bar width=0.7cm,
xtick style={draw=none} ]
\addplot[ybar, black,fill=black!30!white] coordinates {
($p$[\texttt{A}]=0.137, 0.137)
($p$[\texttt{N}]=0.488, 0.488)
};
\coordinate (A) at (axis cs:Plain,0.5);
\coordinate (Alegend) at (axis cs:Plain,0.48);
\coordinate (B) at (axis cs:Plain,0.14);
\coordinate (Blegend) at (axis cs:Plain,0.160);
\coordinate (C) at (axis cs:Plain,0.13);
\coordinate (Clegend) at (axis cs:Plain,0.110);
\coordinate (O1) at (rel axis cs:0,0);
\coordinate (O2) at (rel axis cs:1,0);
\coordinate (O3) at (rel axis cs:0.5,0);
\draw [red,sharp plot,dashed] (A -| O1) -- (A -| O2);
\draw [red,sharp plot,dashed] (B -| O1) -- (B -| O2);
\draw [red,sharp plot,dashed] (C -| O1) -- (C -| O2);
\node[] at (Alegend -| O3) {\color{red}\footnotesize confidence=0.50};
\node[] at (Blegend -| O3) {\color{red}\footnotesize confidence=0.86};
\node[] at (Clegend -| O3) {\color{red}\footnotesize confidence=0.87};
\end{axis}
\end{tikzpicture}
\end{document}
当我编译它时,我收到很多这样的错误:
! Package pgfplots Error: Sorry, the input coordinate `Plain' has not been defi
ned with 'symbolic x coords={$p$[\texttt {A}]=0.137, $p$[\texttt {N}]=0.488}...
Maybe it has been misspelled? Or did you mean something like [normalized]Plain
?.
See the pgfplots package documentation for explanation.
Type H <return> for immediate help.
...
l.44 \end{axis}
?
但是,如果我只是点击Enter
并继续,最终会产生这个情节:
这正是我想要的外观。但是我如何才能获得它而不收到错误消息?我不知道。
该问题与以下内容相关:如何使 pgfplots 条形图不与 y 轴融合?,所以这种行为的解释是,我最初使用 Overleaf 制作它,它只是吞下所有错误消息,你可能知道... :/
答案1
正如下面的评论中所写,这个问题Plain
造成了麻烦。在这里,我提出了一个完全不依赖的解决方案symbolic coords
,应该更容易理解。我还大大简化了绘制水平线的过程。
有关更多详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{xparse}
% define a command for drawing the horizontal lines
% optional argument: add options to the node
% 1st mandatory argument: y-value of the horizontal line
% 2nd mandatory argument: confidence number
\NewDocumentCommand\HLine{O{}mm}{
\draw [red,dashed]
({rel axis cs:0,0} |- {axis cs:0,#2}) --
node [font=\footnotesize,below,#1] {confidence=#3}
({rel axis cs:1,0} |- {axis cs:0,#2});
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% only show ticks at x values that are used ...
xtick=data,
% ... and assign these labels to them
xticklabels={
$p$[\texttt{A}]=0.137,
$p$[\texttt{N}]=0.488,
},
axis x line*=bottom,
axis y line=left,
enlarge x limits=0.2,
ytick={0.15, 0.30, 0.45},
ylabel=$p$-value,
x=3.5cm,
ymin=0,
ymax=0.55,
bar width=0.7cm,
xtick style={draw=none},
]
\addplot[ybar, black,fill=black!30!white] coordinates {
% replaced the symbolic coords with numbers
(0, 0.137)
(1, 0.488)
};
% use the above definition to add the horizontal lines
\HLine{0.50}{0.50}
\HLine[above]{0.14}{0.86}
\HLine{0.13}{0.87}
\end{axis}
\end{tikzpicture}
\end{document}