[Tex/LaTex] Fine tune on histogram pgfplot

histogrampgfplots

Consider the following plot (MWE below):

enter image description here

I have some questions.

  1. [solved] Why is there an extra 1 after the interval [10,11)?
  2. [see edit below] How to make the last interval to be closed? I mean, I'd like to have [9,10] with value 4 (I just put [10,11) to count the last occurrence).
  3. [solved] If possible, I'd like to write the values exactly over the bars, I mean, aligned at the centre (move a little bit to right).

I tried to find some similar solution but no progress.

MWE

\documentclass[border=1pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{%
  compat = 1.12, % I am not sure
  major grid style = {color = blue!80, line width = .6pt},
  every axis title/.append style = {font = \bfseries},
  }

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  title={Title Here},
  ybar interval,
  height = 6cm,
  width = 18cm,
  xmajorgrids = false,
  ymajorgrids = true,
  major tick length = 0pt,
  minor tick length = 0pt,
  xticklabel={%
    $ [ \pgfmathprintnumber\tick , \pgfmathprintnumber\nexttick ) $%
  },
  axis y line = left,
  axis x line = bottom, 
  every outer y axis line/.append style = {-},
  every outer x axis line/.append style = {-},
  xlabel = {faa},
  xlabel style = {at = {(.5,-.2)}, anchor = north},% I'd like bold also
  ylabel = {foo},
  every node near coord/.append style = {%
    anchor = south west,
    black,
    % I'd like bold/italic
  },
  nodes near coords,
  ymin = 0, ymax = 9,
  xmin = 0, xmax = 11.5,
]
%%
\addplot+[%
  hist={% bins = 9, data max = 10  destroy everything
    bins = 10,
    data min = 1,
    data max = 11,
  },
  fill = cyan!50,
]
table[row sep=\\, y index=0]{%
  data\\%
  1\\%
  2\\2\\%
  3\\3\\3\\%
  4\\4\\%
  5\\5\\5\\5\\5\\5\\5\\5\\%
  6\\6\\6\\6\\6\\6\\%
  7\\7\\7\\7\\%
  8\\8\\8\\%
  9\\9\\9\\
  10\\%
};
\end{axis}
\end{tikzpicture}
\end{document}

Edit: for 2. I did this trick not so pretty but it fitted like a glove.

\node[fill=white,anchor=north,inner sep=1pt] at (11.05,-2pt) {$[9,10]$};

enter image description here

Best Answer

If this answer is useful, please go here and mark those answers which solve the key problem described in the question posted above.

The comment just posted by Salim provides a link to essential guidance for your questions. There, the author of pgfplots wrote that the existence of that extra label was likely to be a bug. Nevertheless, others (esdd and jesse) were able to develop workarounds. I recycled that code here to produce what I believe you were trying to produce, together with the other requests.

Some of the changes include the \pgfplotsset code from the referenced question, specifying ymin and ymax, changing nodes near coords to hist nodes near coords, adding font=\bfseries to the xlabel style definition, changing bins=10 to bins=9 and removing data max = 10.

With these we get:

enter image description here

This is the code:

\documentclass[border=1pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}

%https://tex.stackexchange.com/questions/181061/pgfplots-clipping-everything-outside-a-specific-area
\newcommand*\NNC{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}}
\newcommand*\enlargexlimits{.1} % default value for enlarge x limits 
\pgfplotsset{%
    compat=newest,
    hist nodes near coords/.style={%
    nodes near coords*/.add code={}{\tikzset{every node/.append style={xshift={
                    (\pgfkeysvalueof{/pgfplots/width}-45pt) % every plot is 45pt smaller then the width
                    /(1+2*\enlargexlimits) % correction for enlarge x limits
                    /\pgfkeysvalueof{/pgfplots/hist/bins} % number of bins
                    /2% shift only half of bin width
                }}}},
                nodes near coords={% 
                    \pgfmathparse{
                        \pgfkeysvalueof{/data point/x}<#1*\pgfkeysvalueof{/pgfplots/hist/data max}?%
                        "\noexpand\NNC"%  if true print nodes near coords
                        :% if false suppress the additional node near coords
                    }\pgfmathresult%
                }},
                hist nodes near coords/.default={1}% if you set data max explicitly it will be 1
            }

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  title={Title Here},
  ybar interval,
  ymin=0,
  ymax=10,
  height = 6cm,
  width = 18cm,
  enlarge x limits=\enlargexlimits, %<-- setup for label alignment
  xmajorgrids = false,
  ymajorgrids = true,
  major tick length = 0pt,
  minor tick length = 0pt,
  xticklabel={(\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick)},
  axis y line = left,
  axis x line = bottom, 
  x tick label as interval,
  every outer y axis line/.append style = {-},
  every outer x axis line/.append style = {-},
  xlabel = {faa},
  xlabel style = {at = {(.5,-.2)}, anchor = north,font=\bfseries}, % <-- font=\bfseries
  ylabel = {foo},
  ylabel style = {font=\bfseries}
]
%%
\addplot+[%
  hist={% 
    bins = 9, <-- changed 10 to 9
    data min = 1,
%    data max = 10, % <-- remove
  },
  fill = cyan!50,
  hist nodes near coords % <-- new
]
table[row sep=\\, y index=0]{%
  data\\
  1\\
  2\\2\\
  3\\3\\3\\
  4\\4\\
  5\\5\\5\\5\\5\\5\\5\\5\\
  6\\6\\6\\6\\6\\6\\
  7\\7\\7\\7\\
  8\\8\\8\\
  9\\9\\9\\
  10\\
};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question