考虑以下 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}
这将生成以下图像:
如图所示,每个组(月份)的条形图重叠。我尝试了一下,enlarge x limits
但无法正常工作。我该如何修复它?
答案1
将我对 OP 的评论转化为答案,这样问题就不会得不到解答。
代码
\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}