2

enter image description here

How can I plot a graph with a range of e.g. 45% to 59% , 30% to 55% , 30% to 59% as shown in the figure?

Stefan Pinnow
  • 29,535

2 Answers2

1

If the data represents the uncertain range, one can use error bar. And the easiest way is to overwrite the 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}

Symbol 1
  • 36,855
1

This is an extended answer of the one from Symbol 1, which is very great as a basis.

I just extended it for simpler use (mainly) by

  • defining a cycle list for easy use of different colors
  • using table instead of coordinates.

For more details please have a look at the comments in the code.

% 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}

image showing the result of above code

Stefan Pinnow
  • 29,535