如何在 LaTeX 中绘制条形图,其中条形不从 x 轴开始

如何在 LaTeX 中绘制条形图,其中条形不从 x 轴开始

在此处输入图片描述

我如何绘制范围为例如 45% 到 59%、30% 到 55%、30% 到 59% 的图表,如图所示?

答案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}

该图显示了上述代码的结果

相关内容