3

I am working on a population pyramid. With some sort of hard-code I managed to overlap the ytick-labels of the age in the middle of the graph (but not properly). My question is, how to 1.) suppress the y-tick-labels of one of the axes and 2.) align the y-tick label of the left od rigth y-axis to avoid the overlap of the numbers.

Has someone an idea? Thanky for the help,

Cheers, Christian

Although it isn't a minimal example, I post the whole thing:

\documentclass{article}
\usepackage{longtable}
\usepackage{pgfplots}%\pgfplotsset{compat=1.10}
\usepackage{pgfplotstable}
\usepackage{lmodern}
\usepackage{wasysym}

\begin{document}

\pgfplotstableread[col sep=comma, header=true]{
age,man,woman
0,30,32
1,40,42
2,30,32
3,36,32
4,33,25
5,34,35
6,26,28
7,16,22
8,30,38
9,21,32
10,22,24
11,29,27
12,32,28
13,29,15
14,28,23
15,17,25
16,23,17
17,26,25
18,20,22
19,11,22
20,26,25
21,19,19
22,18,22
23,18,24
24,14,25
25,9,18
26,15,14
27,14,10
28,20,12
29,14,13
30,15,13
31,13,18
32,15,15
33,14,16
34,11,12
35,8,10
36,13,11
37,9,7
38,5,18
39,12,12
40,8,6
41,11,9
42,8,10
43,6,8
44,9,6
45,7,8
46,10,10
47,3,8
48,11,8
49,11,8
50,9,6
51,3,10
52,6,11
53,6,5
54,6,8
55,5,6
56,5,8
57,8,4
58,10,4
59,4,8
60,7,3
61,3,8
62,6,3
63,4,3
64,6,3
65,3,4
66,6,3
67,2,5
68,4,3
69,5,8
70,5,3
71,5,5
72,6,3
73,4,0
74,5,4
75,7,3
76,0,0
77,2,2
78,2,1
79,4,3
80,2,0
81,0,1
82,2,2
83,0,1
84,2,0
85,1,0
86,0,1
87,0,1
88,0,0
89,0,1
90,1,0
91,0,0
92,0,0
93,1,0
94,0,1
}\loadedtable


\begin{tikzpicture} 

%FRAUEN-ACHSE
\begin{axis}[
    name=popaxis, xshift=0.42239cm,
    scale only axis, 
    set layers,
    xtick style={/pgfplots/on layer=axis foreground, very thin, black},
    xbar,
    xmin=0,
    xmax=40,
    ymin=0,
    width=0.5\textwidth, height=0.9\textwidth,
    %x label style={at={(axis description cs:0.7,-0.05)},anchor=north},
    xlabel ={\large{Frauen}},
    axis x line=left,
    axis y line=left,
    ytick = {10,20,...,100},
    ytick align=center,
    ytick style={/pgfplots/on layer=axis foreground, very thin, black},
    yticklabel style={/pgfplots/on layer=axis foreground},
    major grid style={/pgfplots/on layer=axis background},
    xmajorgrids,
%    
    enlarge x limits = {value=0.15,upper},
    axis line style={-},
    clip=false,
    axis on top
]
\addplot[xbar interval][red,fill=red] table[y expr =\coordindex, x expr={\thisrow{woman}}] \loadedtable;%[xbar intervall] 
%
\end{axis}

%MÄNNER_ACHSE
\begin{axis}[ 
at={(popaxis.south west)},anchor=south east, xshift=-0.42239cm,
    scale only axis,
    set layers,
    xtick style={/pgfplots/on layer=axis foreground, very thin, black},
    xbar = 0,
    xmin = 0,
    xmax = 40,
    ymin=0,
    width=0.5\textwidth, height=0.9\textwidth,
    %x label style={at={(axis description cs:0.7,-0.05)},anchor=north}, 
    xlabel = {\large{M\"{a}nner}},  
    axis x line=left,
    axis y line=left,
    x dir=reverse,
%                       
    ytick = {10,20,...,100},    
    ytick align=center,
    ytick pos=right,    %gibt an, ob die Ordinate links od. rechts ist
    ytick style={/pgfplots/on layer=axis foreground, very thin, black},
    yticklabel style={/pgfplots/on layer=axis foreground},
    %ylabel = {\large{Anzahl der Personen}},
    %y label style={at={(axis description cs:0.87,-0.1)},anchor=north west, rotate=270},
%    
    enlarge x limits = {value=0.15,upper}, 
    axis line style={-},
    major grid style={/pgfplots/on layer=axis background},
    xmajorgrids,
    axis on top 
]
\addplot[xbar interval][blue!100,fill=blue!100] table[y expr =\coordindex, x expr={\thisrow{man}}] \loadedtable; 
\end{axis}
\end{tikzpicture}



\end{document}

enter image description here

percusse
  • 157,807
  • To suppress the y-tick-labels just change key ytick to ytick =\empty – Salim Bou Jul 15 '15 at 20:14
  • Hi, yes, that works, but the ticks are gone as well...Christian – Christian Jul 15 '15 at 20:22
  • Try with yticklabels={,,} , take a look to this answer – Salim Bou Jul 15 '15 at 20:28
  • Many thanks, that works! Do you have an idea to align the the y-tick labels horizontally , i.e. to center them one over the other? Thanky, Christian – Christian Jul 15 '15 at 20:40
  • For the second graph you can add xshift=1mm (or what satisfy you) to the key yticklabel style – Salim Bou Jul 15 '15 at 20:47
  • Thanks, that works as well, but it shifts all labels horizontally without changing the alignment to some sort of center; I mean, that I have some people older than 100 years, so the labels should be centerd in a form, that the first digit of "100" is not over the first digit of "90"; - can you follow? – Christian Jul 15 '15 at 21:07
  • Is there a way to center the y tick labels? @Christian, did you find out? – jmjr Jun 16 '17 at 10:26

1 Answers1

3

Here a solution

\documentclass{article}
\usepackage{longtable}
\usepackage{pgfplots}%\pgfplotsset{compat=1.10}
\usepackage{pgfplotstable}
\usepackage{lmodern}
\usepackage{wasysym}

\begin{document}

\pgfplotstableread[col sep=comma, header=true]{
age,man,woman
0,30,32
1,40,42
2,30,32
3,36,32
4,33,25
5,34,35
6,26,28
7,16,22
8,30,38
9,21,32
10,22,24
11,29,27
12,32,28
13,29,15
14,28,23
15,17,25
16,23,17
17,26,25
18,20,22
19,11,22
20,26,25
21,19,19
22,18,22
23,18,24
24,14,25
25,9,18
26,15,14
27,14,10
28,20,12
29,14,13
30,15,13
31,13,18
32,15,15
33,14,16
34,11,12
35,8,10
36,13,11
37,9,7
38,5,18
39,12,12
40,8,6
41,11,9
42,8,10
43,6,8
44,9,6
45,7,8
46,10,10
47,3,8
48,11,8
49,11,8
50,9,6
51,3,10
52,6,11
53,6,5
54,6,8
55,5,6
56,5,8
57,8,4
58,10,4
59,4,8
60,7,3
61,3,8
62,6,3
63,4,3
64,6,3
65,3,4
66,6,3
67,2,5
68,4,3
69,5,8
70,5,3
71,5,5
72,6,3
73,4,0
74,5,4
75,7,3
76,0,0
77,2,2
78,2,1
79,4,3
80,2,0
81,0,1
82,2,2
83,0,1
84,2,0
85,1,0
86,0,1
87,0,1
88,0,0
89,0,1
90,1,0
91,0,0
92,0,0
93,1,0
94,0,1
95,0,0
96,0,0
97,0,0
98,0,0
99,0,0
100,1,1
101,0,0
102,0,0
}\loadedtable


\begin{tikzpicture} 

%FRAUEN-ACHSE
\begin{axis}[
    name=popaxis, xshift=0.42239cm,
    scale only axis, 
    set layers,
    xtick style={/pgfplots/on layer=axis foreground, very thin, black},
    xbar,
    xmin=0,
    xmax=40,
    ymin=0,
    width=0.5\textwidth, height=0.9\textwidth,
    %x label style={at={(axis description cs:0.7,-0.05)},anchor=north},
    xlabel ={\large{Frauen}},
    axis x line=left,
    axis y line=left,
    ytick = {10,20,...,90},
    yticklabels={,,}, 
    extra y ticks={100},
    ytick align=center,
    ytick style={/pgfplots/on layer=axis foreground, very thin, black},
    yticklabel style={/pgfplots/on layer=axis foreground},
    major grid style={/pgfplots/on layer=axis background},
    xmajorgrids,
%    
    enlarge x limits = {value=0.15,upper},
    axis line style={-},
    clip=false,
    axis on top
]
\addplot[xbar interval][red,fill=red] table[y expr =\coordindex, x expr={\thisrow{woman}}] \loadedtable;%[xbar intervall] 
%
\end{axis}

%MÄNNER_ACHSE
\begin{axis}[ 
at={(popaxis.south west)},anchor=south east, xshift=-0.42239cm,
    scale only axis,
    set layers,
    xtick style={/pgfplots/on layer=axis foreground, very thin, black},
    xbar = 0,
    xmin = 0,
    xmax = 40,
    ymin=0,
    width=0.5\textwidth, height=0.9\textwidth,
    %x label style={at={(axis description cs:0.7,-0.05)},anchor=north}, 
    xlabel = {\large{M\"{a}nner}},  
    axis x line=left,
    axis y line=left,
    x dir=reverse,
%          
    ytick = {10,20,...,90},
    extra y ticks={100},   
    extra y tick labels={\hspace*{-5pt}100},   
    ytick align=center,
    ytick pos=right,    %gibt an, ob die Ordinate links od. rechts ist
    ytick style={/pgfplots/on layer=axis foreground, very thin, black},
    yticklabel style={/pgfplots/on layer=axis foreground,xshift=3pt},
    %ylabel = {\large{Anzahl der Personen}},
    %y label style={at={(axis description cs:0.87,-0.1)},anchor=north west, rotate=270},
%    
    enlarge x limits = {value=0.15,upper}, 
    axis line style={-},
    major grid style={/pgfplots/on layer=axis background},
    xmajorgrids,
    axis on top 
]
\addplot[xbar interval][blue!100,fill=blue!100] table[y expr =\coordindex, x expr={\thisrow{man}}] \loadedtable; 
\end{axis}
\end{tikzpicture}



\end{document}
  • You can suppress the y-tick-labels from right graph with yticklabels={,,}
  • To align yticklabels just add xshift=length to the key yticklabel style
  • Add tick 100 with extra y ticks={100} and its label with extra y tick labels={\hspace*{length}100}. here we change position of single label 100
Salim Bou
  • 17,021
  • 2
  • 31
  • 76
  • Well, many thanks, that's marvellous! It was (in combination with the suppression of the ytick-labels) the answer I sought.

    With the following combination I was able to add a "0" below as well:

    extra y ticks={0,100},
    extra y tick labels={\hspace{3pt}0,\hspace{-3pt}100}

    Cheers, Christian

    – Christian Jul 16 '15 at 08:59