我正在尝试绘制像这样的明确误差线:
\begin{tikzpicture}
\begin{axis} [symbolic x coords={Lighting,Computers,Total}]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
(Lighting,0.12) +- (0.31,0.03)
(Computers,0.06) +- (0.12,0.01)
(Total,0.07) +- (0.14,0.02)
};
\end{axis}
\end{tikzpicture}
但这会产生一个常量错误,而不是我提供的明确值。例如,对于 Lighting,它会产生 0.12 +-0.03(见下文)。我误读了手册吗?!x 轴上的标签也有些奇怪,我宁愿不要让 y 轴使用 y<0.1 的指数表示法。(我尝试发布输出,但显然我需要更多的信誉!)
答案1
从 PGFPlots 1.9 版开始,直接支持非对称误差线:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotstableread{
x y y-max y-min
Lighting 0.12 0.31 0.03
Computers 0.06 0.12 0.01
Total 0.07 0.14 0.02
}{\mytable}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
ymin=0,
symbolic x coords={Lighting,Computers,Total},
xtick=data
]
\addplot [only marks]
plot [error bars/.cd, y dir=both, y explicit]
table [y error plus=y-max, y error minus=y-min] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
更新
从(至少) 开始,通过 和 来支持非对称version 1.12.1
误差值:pgfplots
y error plus
y error minus
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread{
x y y-max y-min
Lighting 0.12 0.31 0.03
Computers 0.06 0.12 0.01
Total 0.07 0.14 0.02
}{\mytable}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\begin{axis} [symbolic x coords={Lighting,Computers,Total},xtick=data]
\addplot+[forget plot,only marks]
plot[error bars/.cd, y dir=both, y explicit]
table[x=x,y=y,y error plus expr=\thisrow{y-max},y error minus expr=\thisrow{y-min}] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}
旧版本pgfplots
我读过pgfplots
但据我所知,不支持非对称误差线(误差线将使用相同的值);一种可能的解决方法是使用两个 addplots:一个用于上限误差值,另一个用于下限误差值;在下面的例子中,我使用\pgfplotstableread
了pgfplotstable
包来存储数据并轻松重用它。这里有一个小例子:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread{
x y y-max y-min
Lighting 0.12 0.31 0.03
Computers 0.06 0.12 0.01
Total 0.07 0.14 0.02
}{\mytable}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\begin{axis} [symbolic x coords={Lighting,Computers,Total},xtick=data]
\addplot+[forget plot,only marks]
plot[error bars/.cd, y dir=plus, y explicit]
table[x=x,y=y,y error expr=\thisrow{y-max}] {\mytable};
\addplot+[only marks,xticklabels=\empty]
plot[error bars/.cd, y dir=minus, y explicit]
table[x=x,y=y,y error expr=\thisrow{y-min}] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
以下是我的尝试猜测你的问题是什么。从评论来看,包含一个完全可编译的平均能量损失。
\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\begin{document}
\begin{tikzpicture}
\begin{axis} [%log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total}]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
(Lighting,0.12) +- (0.31,0.03)
(Computers,0.06) +- (0.12,0.01)
(Total,0.07) +- (0.14,0.02)
};
\end{axis}
\end{tikzpicture}
\end{document}
这是输出。(不过我仍然鼓励您发布图片,以便我们了解实际情况。)
注意重复的标签。
为了解决这个问题,我手动将宽度设置得更小。(我认为默认值是7 cm
。)在这里,我使用了width=6.5cm
。
\begin{tikzpicture}
\begin{axis} [%log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total},width=6.5cm]% Set the width smaller
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
(Lighting,0.12) +- (0.31,0.03)
(Computers,0.06) +- (0.12,0.01)
(Total,0.07) +- (0.14,0.02)
};
\end{axis}
\end{tikzpicture}
这是我得到的。
消除指数运算的一种解决方法y-axis
是使用{semilogyaxis}
withlog ticks with fixed point
选项。
\begin{tikzpicture}
\begin{semilogyaxis} [log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total},width=6.5cm]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
(Lighting,0.12) +- (0.31,0.03)
(Computers,0.06) +- (0.12,0.01)
(Total,0.07) +- (0.14,0.02)
};
\end{semilogyaxis}
\end{tikzpicture}
输出如下:
编辑
这是使用该xtick=data,ytick=ydata
选项的另一个代码片段。
\begin{tikzpicture}
\begin{semilogyaxis} [log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total},xtick=data,ytick=data]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
(Lighting,0.12) +- (0.31,0.03)
(Computers,0.06) +- (0.12,0.01)
(Total,0.07) +- (0.14,0.02)
};
\end{semilogyaxis}
\end{tikzpicture}