4

I would like to make a pgfplots ybar interval plot with the interval extending to the left of the marked point.

Here is a minimal non-working example:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,xmax=5,
            ymin=0,ymax=1
        ]
            \addplot+[ybar interval,fill=blue!50] coordinates {(1,1) (2,0.25) (3,0.11111) (4,0.0625) (5,0.04)};            
        \end{axis}
    \end{tikzpicture}
\end{document}

sample code output

As you can see, the intervals extend to the right. I would rather it look like this:

desired output

without having to do this (how I produced the above):

\documentclass{standalone}
\usepackage{pgfplots}    
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,xmax=5,
            ymin=0,ymax=1
        ]
            \addplot+[ybar interval,forget plot,fill=blue!50,mark=none] coordinates {(0,1) (1,0.25) (2,0.11111) (3,0.0625) (4,0.04) (5,0)};            
            \addplot+[only marks] coordinates {(1,1) (2,0.25) (3,0.11111) (4,0.0625) (5,0.04)};
        \end{axis}
    \end{tikzpicture}
\end{document}

Is there a key that I can set, or another, cleaner way to get what I want?

  • But your second plot is not what the data says. Is it intentional? – percusse Oct 16 '14 at 20:17
  • I'm not sure what you mean by that. I do want the diagram to look like the second graphic. – Matthew Leingang Oct 17 '14 at 0:47
4

Since all your bars have the same width, there's no reason to use ybar interval. Instead, you can use ybar, bar width=1, bar shift=-0.5 (this requires \pgfplotsset{compat=1.8} or greater).

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,xmax=5,
            ymin=0,ymax=1,
            bar width=1,
            bar shift=-0.5
        ]
            \addplot+[ybar,fill=blue!50] coordinates {(1,1) (2,0.25) (3,0.11111) (4,0.0625) (5,0.04)};            
        \end{axis}
    \end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.