3

I am struggling with a Tikz figure. As you can se in the picture, there are three different Histograms, each for a specific Survey. The problem is that i can't fit the first and the second histogram into the figure, possibly by reducing the gap between each histogram, as well as the their respective x labels. Changing the axis width does not help. It would be also nice to add 2 vertical lines, to separate the three histograms. Do anyone of you know how to solve this problem? Thank you in advance

enter image description here

\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}
  • Can you provide a complete minimal code that is compilable without manual changes. And is the \testmacro code really necessary in order to explain the problem? – Dr. Manuel Kuehner Mar 27 '17 at 21:15
2

You can modify enlarge x limits. In axis units, the distance between the centers of the categories is 1, so a suitable distance from the first/last x-coordinate to the edge of the axis is 0.5. Remember that the bars in a category is centered on the x-coordinate. And to do that, you can add

enlarge x limits={abs=0.5}

to the axis options.

For the vertical lines, seeing as you have already added minor ticks between the categories, you just need to activate the grid, with xminorgrids.

enter image description here

\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}

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.