5

I have a problem with creating a bar plot of demand during a day. My x-coordinates are in "minutes from midnight", but I want to show the time of day on the x-axis at my specified spots (e.g., 6:30 am instead of 390 minutes). xtick and xticklabelsshould do this for me. And they do, when I do not have ybar interval=1, but then it is no longer the right type of plot.

My code is the following:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xtick={390,540,720,900,1020},
            xticklabels={6:30,9:00,12:00,15:00,17:00},
            ylabel=Required staff,
            xlabel=Time of day,
            ybar interval=1,
            width=0.8\textwidth,
            height=5cm,
            axis lines=left,
            ymin=0
        ]
            \addplot 
                coordinates {(390,1) (450,2) (510,3) (570,6) (720,3) (735,2) (780,3) (795,4) (930,2) (960,1) (1020,1)};
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

and it produces:

Ticks are placed incorrectly

Outcommenting ybar interval=1, results in:

Ticks are correct but plot type is wrong

0

2 Answers 2

6

The manual (pag. 87 and 320 in my version) says that ybar interval is installed only on a per-plot basis. It messes with the ticks definition if you set it in the axis.

I would add a enlarge x limits=0.05, enlarge y limits=upper to the axis options, to have a bit of breathing space on the axis.

To use the deault coloring for the diagrams, you can use the generic ybar style, and then addplot+:

\documentclass{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.13}
\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            ybar, %% installs bar cycle list also
            xtick={390,540,720,900,1020},
            xticklabels={6:30,9:00,12:00,15:00,17:00},
            ylabel=Required staff,
            xlabel=Time of day,
            width=0.8\textwidth,
            height=5cm,
            axis lines=left,
            ymin=0,
            enlarge x limits=0.05,
            enlarge y limits=upper,
        ]
        \addplot+ [ybar interval]
                coordinates {(390,1) (450,2) (510,3) (570,6) (720,3) (735,2) (780,3) (795,4) (930,2) (960,1) (1020,1)};
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

another bar plot

3
  • Thank you. A simpler solution than an extra axis, to be sure, but it is rather annoying that you have to fill the bars yourself... You shouldn't happen to know where I can find the name of the standard bar colour? Also, is there a special reason that you are using compat=1.11 instead of compat=1.14?
    – TracedWill
    Apr 26, 2017 at 9:37
  • Well... it's just a fill=color to add. I think this is the way it is supposed to be used, and the problem you met is a quirk when not used as explained in the manual (you can check it by using ybar interval in the axis option before setting the ticks label, and see thing changing). About the compat level, no, it's just that I typed 1.11 out of habits... and I have 1.13 here.
    – Rmano
    Apr 26, 2017 at 9:40
  • For the color, I found it: it's in the bar cycle list, --- will change the answer shortly
    – Rmano
    Apr 26, 2017 at 10:33
3

You could add a second axis environment with the same dimensions:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}% <- current version is 1.14
\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \pgfplotsset{
            xmin=390,xmax=1020,
            width=0.8\textwidth,
            height=5cm,
            axis lines=left,
            ymin=0,ymax=6
        }
        \begin{axis}[
            xtick={390,540,720,900,1020},
            xticklabels={6:30,9:00,12:00,15:00,17:00},
            ylabel=Required staff,
            xlabel=Time of day
        ]
        \end{axis}
        \begin{axis}[
            axis lines=none,
            ybar interval=1,
            xtick=\empty,
        ]
            \addplot 
                coordinates {
                    (390,1) (450,2) (510,3) (570,6) (720,3) (735,2)
                    (780,3) (795,4) (930,2) (960,1) (1020,1)
                };
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

1
  • It seems weird that two axis environments are needed to do this... But it works, thanks!
    – TracedWill
    Apr 26, 2017 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .