Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a const plot with intervals on the y-axis. I'd like my y labels to be centered in each bar, as it would be if this were an xbar interval graph.

Also, and more importantly, I'd like more flexibility and control in the y label intervals. Can I, for instance, include every other interval, as in [0,.05) [.1,.15) etc., with that gap in between?

Finally, is there an easy way to make the last interval a closed bracket on both sides?

\documentclass{article}
\usepackage{pgfplots}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[
   height=10cm,
        const plot,
        y tick label as interval=true,
        ytick=,
        yticklabel={$[\pgfmathprintnumber\tick, \pgfmathprintnumber\nexttick)$},
        enlargelimits=false,
        ymin=0,
        xmin=0
    ] 
\addplot [fill=blue,opacity=.6,draw=blue]
coordinates
{(0,0) (393,0.000) (93,0.050) (44,0.100) (49,0.150) (31,0.200) (32,0.250) (37,0.300) (52,0.350) (90,0.400) (30,0.450) (26,0.500) (11,0.550) (32,0.600) (31,0.650) (15,0.700) (33,0.750) (18,0.800) (25,0.850) (31,0.900) (162,0.950) (0,1)};
\end{axis}
\end{tikzpicture}


\end{document}
share|improve this question
    
I should say that I'm not using xbar interval because I'm adding another plot, and I prefer no gaps between bars for both plots. –  PaulB Feb 4 '13 at 21:20
    
If you want to add something to your question as you do in the comment above, you can just edit your question to add it there. This is better than adding such things in the comments as they are less likely to be read. –  Benedikt Bauer Feb 4 '13 at 21:25
    
I think that just about does it percuße, but I'll have to also shift my coordinates as that gives me from [0,.05) to [.9,.95) –  PaulB Feb 4 '13 at 21:29

1 Answer 1

up vote 4 down vote accepted

To get one label for each bar, you can set ytick=data. To get the closed interval, you can use an inline check to see if the upper bound of the bin is 1, using \ifdim\nexttick pt=1pt ] \else ) \fi (\ifdim needs the values it compares to have units, that's why we'll use pt). To make sure everything aligns nicely, you should use \pgfmathprintnumber[fixed, fixed zerofill]{...}, and print the square bracket using \leavevmode\rlap{]}\hphantom{)} (as egreg kindly told me).

To print only some labels, ordinarily you'd use ytick={0,0.1,...,1}, but in the case of interval ticks that doesn't do what you want it to (it would plot 0-0.1, 0.1-0.2, while you want 0-0.05,0.1-0.15), so we'll have to do something else: We can introduce a counter that is incremented for every tick, and at each tick we check whether the counter value is divisible by 2 (or 3 or whatever) using \pgfmathparse. If it is, we print the tick label, if it isn't we don't:

\documentclass{article}
\usepackage{pgfplots}


\begin{document}

\newcounter{ticknumber}
\setcounter{ticknumber}{1}
\makeatletter
\begin{tikzpicture}
    \begin{axis}[
   height=10cm,
        const plot,
        y tick label as interval=true,
        ytick=data,
        yticklabel={%
        \pgfmathtruncatemacro\isdivisible{Mod(\theticknumber,2)==0}
        \ifnum\isdivisible=1
         $[\pgfmathprintnumber[fixed, fixed zerofill]\tick, \pgfmathprintnumber[fixed, fixed zerofill]\nexttick\ifdim\nexttick pt=1pt \leavevmode\rlap{]}\hphantom{)} \else ) \fi$
        \fi
        \stepcounter{ticknumber}
        },
        enlargelimits=false,
        ymin=0,
        xmin=0
    ] 
\addplot [fill=blue,opacity=.6,draw=blue]
coordinates
{(0,0) (393,0.000) (93,0.050) (44,0.100) (49,0.150) (31,0.200) (32,0.250) (37,0.300) (52,0.350) (90,0.400) (30,0.450) (26,0.500) (11,0.550) (32,0.600) (31,0.650) (15,0.700) (33,0.750) (18,0.800) (25,0.850) (31,0.900) (162,0.950) (0,1)};
\end{axis}
\end{tikzpicture}


\end{document}
share|improve this answer
    
This definitely answers the question I originally asked, but it leads me to another. I'll modify my original question. –  PaulB Feb 4 '13 at 21:31
    
@PaulB: I've edited my answer –  Jake Feb 4 '13 at 22:05
    
Awesome! thank you! –  PaulB Feb 4 '13 at 22:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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