1

Consider the following MWE

\documentclass[tikz]{standalone}
\usepackage{lmodern}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
 \usepackage{pgfplotstable}


\begin{document}
\pgfplotstableread[col sep=comma]{
month, 2012, 2013, 2014
January, 112798, 110052, 108772
February, 58183, 63319, 58303
March, 82263, 77653, 51626
April, 60834, 82041, 52901
May, 76389, 92386, 55931
June, 69716, 74734, 51540
July, 74161, 87917, 61273
August, 76480, 82330, 57653
September, 64509, 84757, 58516
October, 70193, 88920,
November, 58096, 61552,
December, 37551, 49362,
}\datatable
\begin{tikzpicture}
\begin{axis}[
    % Bar plot and axis settings
    ybar,
    axis lines*=left,
    ymajorgrids,
    bar width=0.26cm,
    % Y ticks and label
    ymin=0, ymax=150000,
    ytick={0,20000,40000,60000,80000,100000,120000},
    y tick label style={font=\footnotesize},
    ylabel= Unidades,
    % X ticks
    xtick=data,
    x tick label style={font=\footnotesize,align=right,rotate=90},
    every tick/.style={color=black, line width=0.35pt},
    xticklabels from table={\datatable}{month},
    enlarge x limits=0.1,
]
\pgfplotsinvokeforeach {1,...,3}{
    \addplot table [x expr=\coordindex, y index=#1] {\datatable};
}
\end{axis}
\end{tikzpicture}
\end{document}

This generates the following image:

enter image description here

As it can be seen in the image, the bars for each group (month) overlap. I tried playing with enlarge x limits but cannot make it work. How can I fix it?

  • Try width=1.2\textwidth, or 1\textwidth but with 0.2 bar width – Jesse Oct 20 '14 at 14:46
  • @Jesse thanks that seems to work. How do I increase the separation ?between each group (month) of plots? – petobens Oct 20 '14 at 14:49
  • I used ybar=xx pt. In your case, ybar=10pt seems fair. – Jesse Oct 20 '14 at 14:54
  • @Jesse mmm that doesn't exactly work the way I want. I would like the bars within each month to be together but those of different months separated. – petobens Oct 20 '14 at 14:56
  • Did you increase width to, say, 2\textwidth. But with 12 months, each has 3 bars may need scale option. – Jesse Oct 20 '14 at 15:01
1

Turn my comments with OP into an answer so that the question won't be unanswered.

enter image description here

Code

\documentclass[tikz,border=10pt]{standalone}
\usepackage{lmodern}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
 \usepackage{pgfplotstable}


\begin{document}
\pgfplotstableread[col sep=comma]{
month, 2012, 2013, 2014
January, 112798, 110052, 108772
February, 58183, 63319, 58303
March, 82263, 77653, 51626
April, 60834, 82041, 52901
May, 76389, 92386, 55931
June, 69716, 74734, 51540
July, 74161, 87917, 61273
August, 76480, 82330, 57653
September, 64509, 84757, 58516
October, 70193, 88920,
November, 58096, 61552,
December, 37551, 49362,
}\datatable
\begin{tikzpicture}
\begin{axis}[
    % Bar plot and axis settings
    ybar,
    axis lines*=left,%scaled y ticks=false,
    ymajorgrids,
    bar width=0.26cm, width=1.45\textwidth,
    % Y ticks and label
    ymin=0, ymax=150000,
    ytick={0,20000,40000,60000,80000,100000,120000},
    y tick label style={font=\footnotesize},
    ylabel= Unidades,
    % X ticks
    xtick=data,
    x tick label style={font=\footnotesize,align=right,rotate=90},
    every tick/.style={color=black, line width=0.35pt},
    xticklabels from table={\datatable}{month},
    enlarge x limits=0.1,
]
\pgfplotsinvokeforeach {1,...,3}{
    \addplot table [x expr=\coordindex, y index=#1] {\datatable};
}
\end{axis}
\end{tikzpicture}
\end{document}

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.