我正在努力制作一个 Tikz 图形。正如您在图片中看到的,有三个不同的直方图,每个直方图都针对特定的调查。问题是我无法将第一个和第二个直方图放入图中,可能是通过减少每个直方图之间的间隙以及它们各自的 x 标签来实现的。更改轴宽没有帮助。添加 2 条垂直线来分隔三个直方图也不错。你们中有人知道如何解决这个问题吗?提前谢谢
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
},
}
\pgfplotstableread{
0 62 6 6 6 9 2 2 2 0 3
1 53 13 10 5 6 3 2 2 1 4
2 58 15 11 5 5 1 1 1 1 1
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
width=16cm,
height=8cm,
ymajorgrids,
ymin=0,
ymax=70,
ylabel={\%},
xtick=data,
xticklabels = {
\textbf{1989-1993},
\textbf{1994-1998},
\textbf{2005-2009},
},
xticklabel style={yshift=-3ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=1ex,
every node near coord/.append style={
anchor=north,
}
]
\addplot[draw=black,fill=black!30, nodes near coords=1] table[x index=0,y index=1] \dataset; %Data1
\addplot[draw=black,fill=black!30, nodes near coords=2] table[x index=0,y index=2] \dataset; %Data2
\addplot[draw=black,fill=black!30, nodes near coords=3] table[x index=0,y index=3] \dataset; %Data3
\addplot[draw=black,fill=black!30, nodes near coords=4] table[x index=0,y index=4] \dataset; %Data4
\addplot[draw=black,fill=black!30, nodes near coords=5] table[x index=0,y index=5] \dataset; %Data5
\addplot[draw=black,fill=black!30, nodes near coords=6] table[x index=0,y index=6] \dataset; %Data6
\addplot[draw=black,fill=black!30, nodes near coords=7] table[x index=0,y index=7] \dataset; %Data7
\addplot[draw=black,fill=black!30, nodes near coords=8] table[x index=0,y index=8] \dataset; %Data8
\addplot[draw=black,fill=black!30, nodes near coords=9] table[x index=0,y index=9] \dataset; %Data9
\addplot[draw=black,fill=black!30, nodes near coords=10] table[x index=0,y index=10] \dataset; %Data10
\end{axis}
\end{tikzpicture}
答案1
您可以修改enlarge x limits
。以轴单位表示,类别中心之间的距离为 1,因此从第一个/最后一个 x 坐标到轴边缘的合适距离为 0.5。请记住,类别中的条形以 x 坐标为中心。为此,您可以添加
enlarge x limits={abs=0.5}
到轴选项。
对于垂直线,由于您已经在类别之间添加了小刻度,因此您只需使用 激活网格即可xminorgrids
。
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
},
}
\pgfplotstableread{
0 62 6 6 6 9 2 2 2 0 3
1 53 13 10 5 6 3 2 2 1 4
2 58 15 11 5 5 1 1 1 1 1
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
width=16cm,
height=8cm,
ymajorgrids,
xminorgrids, %% added
ymin=0,
ymax=70,
ylabel={\%},
xtick=data,
xticklabels = {
\textbf{1989-1993},
\textbf{1994-1998},
\textbf{2005-2009},
},
xticklabel style={yshift=-3ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=1ex,
every node near coord/.append style={
anchor=north
},
enlarge x limits={abs=0.5} %% added
]
\addplot[draw=black,fill=black!30, nodes near coords=1] table[x index=0,y index=1] \dataset; %Data1
\addplot[draw=black,fill=black!30, nodes near coords=2] table[x index=0,y index=2] \dataset; %Data2
\addplot[draw=black,fill=black!30, nodes near coords=3] table[x index=0,y index=3] \dataset; %Data3
\addplot[draw=black,fill=black!30, nodes near coords=4] table[x index=0,y index=4] \dataset; %Data4
\addplot[draw=black,fill=black!30, nodes near coords=5] table[x index=0,y index=5] \dataset; %Data5
\addplot[draw=black,fill=black!30, nodes near coords=6] table[x index=0,y index=6] \dataset; %Data6
\addplot[draw=black,fill=black!30, nodes near coords=7] table[x index=0,y index=7] \dataset; %Data7
\addplot[draw=black,fill=black!30, nodes near coords=8] table[x index=0,y index=8] \dataset; %Data8
\addplot[draw=black,fill=black!30, nodes near coords=9] table[x index=0,y index=9] \dataset; %Data9
\addplot[draw=black,fill=black!30, nodes near coords=10] table[x index=0,y index=10] \dataset; %Data10
\end{axis}
\end{tikzpicture}
\end{document}