我正在创建一个组图(至少打算这样做),每个图的宽度为 4 厘米。
因为我希望通过 显示值nodes near coords
,但这是有问题的,因为我不知道如何enlarge x limits
正确使用。它要么将条形图推得太近,导致值高于其他值,要么高于其他条形图。另一种情况是,当使用 时{0.2,upper}
,将条形图向左移动太多。
是否有一个合理的方法,即不只是进行 T&E 运行直到其合适?
(此外,整个过程似乎都依赖于字体,但不幸的是我到目前为止还无法重现这一点。)
图片
平均能量损失
\documentclass[
a4paper
]{scrartcl}
\usepackage{
lmodern,
amsmath,
tikz,
pgfplots,
pgfplotstable
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{filecontents}{force-stats.txt}
Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
Fx,-7.35,2,2.26,6,1,1
Fy,-4,0.95,0.76,5,1.91,1.5
Fz,-2,1.42,1,4,4,3
\end{filecontents}
\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
scale only axis=true,
width=4cm,
height=6cm,
%
xlabel={Force components},
ylabel={Force in N},
%
ybar,
%
enlarge x limits={0.3},
%
symbolic x coords={Fx,Fy,Fz},
%
xtick=data,
nodes near coords,
xticklabels={{\(F_{x}\)},{\(F_{y}\)},{\(F_{z}\)}},
%
legend columns={1},
legend style={at={(0,0)},anchor=north,at={(axis description cs:0.5,-0.18)}}
]
\addplot+[nodes near coords align={above right}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Max, y error=MinCIP95] {\tableabcdef};
\addlegendentry{$F_i^{\text{max}}$, a very long one for the problem}
\addplot+[nodes near coords align={below right}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Min, y error=MinCIP95] {\tableabcdef};
\addlegendentry{$F_i^{\text{min}}$, test text text}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
也许你可以使用类似
enlarge x limits={abs={11pt+1ex}},
enlarge y limits={abs={5pt+1em}},
标准bar width
为 10pt,条形之间的标准偏移为 2pt。因此,该enlarge x limits
abs
值必须至少为 11pt。
应该enlarge y limits
考虑到 sep(在下面的例子中为 5pt)和 的高度nodes near coords
。
代码:
\documentclass{scrartcl}
\usepackage{
amsmath,
pgfplots,
pgfplotstable
}
\usepackage{tgheros}
\usepackage[scaled=0.9]{beramono}
\usepackage{newpxtext,newpxmath}
\usepackage{amsmath}
\usepackage{sansmath}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{filecontents}{force-stats.txt}
Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
Fx,-7.35,2,0.36,6,1,0.4
Fy,-4,0.95,0.76,5,1.91,0.5
Fz,-2,1.42,0.5,4,4,0.5
\end{filecontents}
\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}[
scale only axis=true,
width=4cm,
height=5cm,
%
xlabel={Force components},
ylabel={Force in N},
%
ybar,
%
enlarge x limits={abs={11pt+1ex}},
enlarge y limits={abs={5pt+1em}},
%
symbolic x coords={Fx,Fy,Fz},
%
xtick=data,
nodes near coords,
xticklabels={{\(F_{x}\)},{\(F_{y}\)},{\(F_{z}\)}},
%
legend columns={1},
legend style={anchor=north,at={(axis description cs:0.5,-0.25)}, legend cell align={left}},
]
\addplot+[nodes near coords align={above=5pt}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Max, y error=MinCIP95] {\tableabcdef};
\addlegendentry{$F_i^{\text{max}}$, a very long one for the problem}
\addplot+[nodes near coords align={below=5pt}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Min, y error=MinCIP95] {\tableabcdef};
\addlegendentry{$F_i^{\text{min}}$, test text text}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
nodes near coords align
此外,我还将图例的位置和上下位置改为5pt。