ytick 标签的水平对齐

ytick 标签的水平对齐

我正在研究人口金字塔。通过某种硬编码,我设法将年龄的 y 轴刻度标签重叠在图表中间(但效果并不好)。我的问题是,如何 1.) 隐藏其中一个轴的 y 轴刻度标签和 2.) 对齐左侧或右侧 y 轴的 y 轴刻度标签以避免数字重叠。

有人有主意吗?谢谢你的帮助,

欢呼吧,克里斯蒂安

尽管这不是一个最小的例子,但我还是发布了整个内容:

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

在此处输入图片描述

答案1

这是一个解决方案

\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}
  • 您可以使用以下方法隐藏右图中的 y 刻度标签: yticklabels={,,}
  • 要对齐 yticklabels,只需添加xshift=length到键yticklabel style
  • 添加勾号 100 extra y ticks={100}并用 为其标签extra y tick labels={\hspace*{length}100}。这里我们更改单个标签 100 的位置

相关内容