6

I have an array of numbers for which I want to create a histogram for the first 22 entries. I want to show both the first few entries of the array as a bar plot as well as the histogram in two subfigures. For the the histogram I tried using pgfplots, ybar interval and addplot+/table starting with the example from the pgfplots-manual and adjusted it to my data.

enter image description here

My problem with the output is that the width of the interval separators is not the same as the width of my histogram bars. This results in seemingly wrong labeled bars, since the histogram is not as wide as the axis. I unsuccessfully tried following attempts to solve the problem:

  1. Using ybar instead of ybar interval - This moves the ticks/labeling to the left bottom of the bar, while I'd like it centered
  2. Not specifying xticks - This leads to the same width for the separators and the histogram bars, but the labeling changes to weird floating point numbers.

Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\begin{subfigure}[b]{0.7\textwidth}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    ybar,
    bar width=7pt,
    height=5 cm,
    width=13cm,
    xtick={1,3,...,30},
    ytick={1,3,...,13},
    xlabel={Frames},
    ylabel={Class}]
    \addplot coordinates {
        (1,12) (2,9) (3,1) (4,11) (5,11) (6,1) (7,1) (8,12) (9,1) (10,12) 
        (11,12) (12,12) (13,12) (14,12) (15,12) (16,12) (17,12) (18,12) (19,12) (20,12) 
        (21,12) (22,13) (23,1) (24,1) (25,1) (26,11) (27,1) (28,1) (29,1)
    };
    \end{axis}
\end{tikzpicture}
\end{subfigure}

\begin{subfigure}[b]{0.7\textwidth}
\centering
\begin{tikzpicture}
    \begin{axis}[
    ybar interval,
    height=5 cm,
    width=13cm,
    xlabel=Class,
    ylabel=Counts,
    xtick={1,...,14},
    ]
    \addplot+[hist={bins=13}]
    table[row  sep=\\,y  index=0]  {
    data\\
    12 \\9 \\1 \\11 \\11 \\1 \\1 \\12 \\1 \\12 \\
    12 \\12 \\12 \\12 \\12 \\12 \\12 \\12 \\12 \\12 \\
    12 \\13 \\
    };
    \end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}

\end{document}
  • By the way, why is your second chart a histogram? Could it not simply be another bar chart? – John Wickerson May 7 '13 at 12:56
  • i'd have to do the counting myself and since i'm having lots of different histograms, i'd like to avoid that. i could let matlab to the counting for me (since it produces the data), but i'd still have to adjust the diagrams. – fukiburi May 7 '13 at 13:11
8

You should do two things:

  • Remove the xtick option

  • Give an explicit data max of 14.

The trick is to think of the thirteen bins not as the numbers 1, 2, ..., 13, but as the half-open intervals [1,2), [2,3), ..., [13,14). That's why the data max needs to be 14, not 13.

Result:

enter image description here

Amended code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\begin{subfigure}[b]{0.7\textwidth}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    ybar,
    bar width=7pt,
    height=5cm,
    width=13cm,
    xtick={1,3,...,30},
    ytick={1,3,...,13},
    xlabel={Frames},
    ylabel={Class}]
    \addplot coordinates {
        (1,12) (2,9) (3,1) (4,11) (5,11) (6,1) (7,1) (8,12) (9,1) (10,12) 
        (11,12) (12,12) (13,12) (14,12) (15,12) (16,12) (17,12) (18,12) (19,12) (20,12) 
        (21,12) (22,13) (23,1) (24,1) (25,1) (26,11) (27,1) (28,1) (29,1)
    };
    \end{axis}
\end{tikzpicture}
\end{subfigure}

\begin{subfigure}[b]{0.7\textwidth}
\centering
\begin{tikzpicture}
    \begin{axis}[
    ybar interval,
    height=5cm,
    width=13cm,
    xlabel=Class,
    ylabel=Counts,
    %xtick={1,...,13},
    ]
    \addplot+[hist={bins=13, data max=14}]
    table[row sep=\\,y index=0]  {
    data\\
    12 \\9 \\1 \\11 \\11 \\1 \\1 \\12 \\1 \\12 \\
    12 \\12 \\12 \\12 \\12 \\12 \\12 \\12 \\12 \\12 \\
    12 \\13 \\
    };
    \end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}

\end{document}
  • Thanks for the quick answer, but I really want to keep 13 bins - with the 13th empty because I have exactly 13 classes. (edit:) Actually, it's not empty. There is one vote. in the 13th bin. – fukiburi May 7 '13 at 12:25
  • Ah yes, my bad. I'll have another think. – John Wickerson May 7 '13 at 12:31
  • Ok, I think I fixed it now -- see revised answer above. – John Wickerson May 7 '13 at 12:48

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.