![如何在 LaTeX 中绘制条形图,其中条形不从 x 轴开始](https://linux22.com/image/352474/%E5%A6%82%E4%BD%95%E5%9C%A8%20LaTeX%20%E4%B8%AD%E7%BB%98%E5%88%B6%E6%9D%A1%E5%BD%A2%E5%9B%BE%EF%BC%8C%E5%85%B6%E4%B8%AD%E6%9D%A1%E5%BD%A2%E4%B8%8D%E4%BB%8E%20x%20%E8%BD%B4%E5%BC%80%E5%A7%8B.png)
答案1
如果数据代表不确定范围,可以使用error bar
。最简单的方法是覆盖draw error bar/.code
。
\documentclass[border=9,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
error bars/draw error bar/.code 2 args={%
\draw[line width=10]#1--#2;
}
}
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
mark=n o n e,
error bars/y dir=both,
error bars/y explicit,
]
coordinates{
(0,0) +- (0.5,0.1)
(0.1,0.1) +- (0.05,0.2)
(0.2,0.2) +- (0,0.05)
(0.5,0.5) +- (0.1,0.2)
(1,1) +- (0.3,0.1)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这是来自的答案的扩展符号 1,作为基础非常棒。
我只是扩展了它以便于使用(主要是)
- 定义一个
cycle list
以便于使用不同的颜色 - 使用
table
而不是coordinates
。
有关更多详细信息,请查看代码中的注释。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% we don't want to see any lines
only marks,
% but also no markers here, so we just append that to each plot
% instead of repeating it everywhere
every axis plot post/.append style={mark=none},
% draw the bar as an error bar ...
error bars/draw error bar/.code 2 args={
\draw [line width=10] #1 -- #2;
},
% ... where the normal coordinate gives the starting point and the
% error value gives the height of the bar
error bars/y dir=plus,
error bars/y explicit,
% create a cycle list for the this special purpose, where we have
% to give the color to the `error bar style'
cycle list={
{error bars/error bar style={blue}},
{error bars/error bar style={red}},
{error bars/error bar style={green}},
},
]
% because it is easier to give the values as a table I prefer this
% solution. Of course you could also give the coordinates in a file.
% (Unfortunaltely it seems that providing `table/yerror=yerror' to
% the axis options doesn't work, so we have to repeat it ...)
\addplot table [y error=yerror] {
x y yerror
0 0 0.1
0.1 0.1 0.2
};
\addplot table [y error=yerror] {
x y yerror
0.2 0.2 0.05
0.5 0.5 0.2
};
\addplot table [y error=yerror] {
x y yerror
1 1 0.1
};
\end{axis}
\end{tikzpicture}
\end{document}