我正在制作如下所示的条形图:
\begin{tikzpicture}[baseline=(current bounding box.center)]
\begin{axis}[ symbolic x coords={$p$[A]=0.137, $p$[N]=0.488},
xtick=data,
axis x line*=bottom,
axis y line=left,
enlarge x limits={abs=bar},
yticklabels={,,},
ylabel=p-value,
x=3.5 cm,
ymin = 0,
ymax = 0.55,
bar width=0.7cm,
tick style={draw=none} ]
\addplot[ybar, black,fill=black!30!white] coordinates {
($p$[A]=0.137, 0.137)
($p$[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}
我希望 y 轴和第一个条之间有一些空间。基于https://tex.stackexchange.com/a/64421/298我虽然enlarge x limits={abs=bar},
可以工作但它什么也没做... :/
编辑:
我离开了 Overleaf,它似乎只是吞下了一些错误,这里有一些在 Overleaf 之外运行的代码:
\documentclass[usenames,dvipsnames,12pt,oneside,article]{memoir}
\usepackage{hyperref}
\usepackage{microtype}
\usepackage{letltxmacro}
\usepackage[lining]{ebgaramond}
\usepackage[cmintegrals,cmbraces]{newtxmath}
\usepackage{ebgaramond-maths}
\usepackage{fontspec}
\usepackage{booktabs}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{enumitem}
\usepackage{rotating}
\usepackage{unicode-math}
\usepackage{booktabs}
\renewcommand{\thefootnote}{\color{red}\arabic{footnote}}
\usetikzlibrary{shapes.geometric, positioning, arrows.meta, decorations.pathreplacing}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.misc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\begin{axis}[ symbolic x coords={$p$[A]=0.137, $p$[N]=0.488},
xtick=data,
axis x line*=bottom,
axis y line=left,
%enlarge x limits={true,abs value=bar},
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=red!30!white] coordinates {
($p$[A]=0.137, 0.137)
($p$[N]=0.488, 0.488)
};
\end{axis}
\end{tikzpicture}
\end{document}
以下也是背面页面的链接:https://www.overleaf.com/read/nnjqjbnvdrct
答案1
我删除了大多数不相关的样式,只添加enlarge x limits=0.2
了
\documentclass[usenames,dvipsnames,12pt,oneside,article]{memoir}
% \usepackage{hyperref}
% \usepackage{microtype}
% \usepackage{letltxmacro}
% \usepackage[lining]{ebgaramond}
% \usepackage[cmintegrals,cmbraces]{newtxmath}
% \usepackage{ebgaramond-maths}
% \usepackage{fontspec}
% \usepackage{booktabs}
\usepackage{amssymb}
\usepackage{tikz}
% \usepackage{enumitem}
% \usepackage{rotating}
% \usepackage{unicode-math}
% \usepackage{booktabs}
\renewcommand{\thefootnote}{\color{red}\arabic{footnote}}
\usetikzlibrary{shapes.geometric, positioning, arrows.meta, decorations.pathreplacing}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.misc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\begin{axis}[ symbolic x coords={$p$[A]=0.137, $p$[N]=0.488},
xtick=data,
axis x line*=bottom,
axis y line=left,
enlarge x limits=0.2,
%enlarge x limits={true,abs value=bar},
ytick = {0.15, 0.30, 0.45},
yticklabels={0.15,0.30,0.45},
ylabel=p-value,
%xmin=-0.2,
x=3.5 cm,
ymin = 0,
ymax = 0.55,
bar width=0.7cm,
xtick style={draw=none} ]
\addplot[ybar, black,fill=red!30!white] coordinates {
($p$[A]=0.137, 0.137)
($p$[N]=0.488, 0.488)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
除了@marmot 给出的解决方案之外,pgfplots v1.16 手册还指出,该问题来自关键symbolic x coords={}
。
我引用手册1.16第385页:
符号坐标很有用,因为它们减轻了将字符串映射到索引和反之亦然的负担。
但是,他们有一个警告:如果你想要设置
xmin
第一个左边的某个东西,该怎么办symbolic x coord?
这是不可能的,因为任何输入坐标都应包含在 中
symbolic x coord
。然而,为了允许这样的修改,pgfplots 会检查魔法前缀[normalized]
:每当 pgfplots 发现符号坐标它以精确的字符串开头
[normalized]
,它会将前缀后面的所有内容解释为规范化的数字,没有符号。在我们的示例中,xmin
将设置为 -1:7,即 a 左侧的某个值(根据上面的定义,它将为 0)。
所以我添加了这个代码:xmin={{[normalized]-0.2},
并且一切正常!
\documentclass[usenames,dvipsnames,12pt,oneside,article]{memoir}
\usepackage{amssymb}
\usepackage{tikz}
\renewcommand{\thefootnote}{\color{red}\arabic{footnote}}
\usetikzlibrary{shapes.geometric, positioning, arrows.meta, decorations.pathreplacing}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.misc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\begin{axis}[ symbolic x coords={$p$[A]=0.137, $p$[N]=0.488},
xtick=data,
axis x line*=bottom,
axis y line=left,
ytick = {0.15, 0.30, 0.45},
yticklabels={0.15,0.30,0.45},
ylabel=p-value,
xmin={[normalized]-0.2},
x=3.5 cm,
ymin = 0,
ymax = 0.55,
bar width=0.7cm,
xtick style={draw=none} ]
\addplot[ybar, black,fill=red!30!white] coordinates {
($p$[A]=0.137, 0.137)
($p$[N]=0.488, 0.488)
};
\end{axis}
\end{tikzpicture}
\end{document}