我正在使用 pgfplots 制作 xbar 图。在男性的不同年龄组(“年龄间隔”)中,根据一些测试选择一个获胜者(获胜者),并对该获胜者测量两个不同的变量(“测量 1”和“测量 2”)。对获胜者的这种双重测量是在几个不同的州/国家进行的。对于每个国家,两个结果都显示为同一行上的两个条形图,请参阅所附代码。
我的问题是:
(1) 如何将每个国家名称放在其旁边 y 轴上的特定垂直位置?例如,我希望国家名称出现在其右侧条形图中下 x 轴和上 x 轴中间的垂直位置。
(2)如何使条形图的列垂直对齐,而不管国家名称的长度如何?
(3)如何将 y 标签(“年龄间隔”和“Winer”)放在与标题“Meas. 1”和“Meas. 2”完全相同的基线上(垂直对齐)?现在我已通过代码行手动调整了它
every axis y label/.style = {at={(yticklabel cs:0.94)}, rotate=0,anchor =center},
所以我想用一些坐标变量来交换“(yticklabel cs:0.94)”作为标题。
(4) 我怎样才能将 y 标签及其下方的 y 刻度标签拉离 y 轴一点?即以“年龄间隔”开头的列和以“测量 1”开头的列之间应该有更多的水平空间。
(5)我想在国家名称列上方添加一个名为“国家”的标题,并且该标题也应与标题“测量 1”和“测量 2”垂直对齐
(6) 我想将上面描述的所有行和列放在“figure”环境中,以便添加标题。如何防止此 figure 环境取消对齐国家名称和条形图的列?
(7)如何确保条形图行之间的水平空间与条形图列之间的垂直空间相同?
我的代码是:
% !TeX program = pdflatex
% !TeX encoding = ISO-8859-1
\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{
compat=newest,
width=4cm,
height=4cm,
title style = {yshift = 10pt},
xtick align = inside,
xtick pos = both,
xticklabel pos = upper,
xmin=-5,
xmax = 140,
ytick align = outside,
ytick pos = left,
yticklabel pos=left,
ymin =0,
ymax = 5,
ytick=data,
xbar,
nodes near coords,
every node near coord/.append style = {anchor=west},
}
% Measurements contry 1
\begin{filecontents}{data1.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 15 45 John
30-40 2 20 13 Al
40-50 3 12 4 Andrew
50-60 4 24 1 Tom
\end{filecontents}
% Measurements contry 2
\begin{filecontents}{data2.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 44 30 Peter
30-40 2 15 33 Steve
40-50 3 2 48 David
50-60 4 66 98 Alister
\end{filecontents}
% Measurements contry 3
\begin{filecontents}{data3.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 13 22 Arne
30-40 2 1 48 Per
40-50 3 2 4 Ola
50-60 4 33 61 Anders
\end{filecontents}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ohio
\begin{tikzpicture}
\begin{axis}[
title = {Meas. 1},
yticklabel pos=left,
yticklabels from table={data1.dat}{Age-interval},
ylabel = {Age interval},
every axis y label/.style = {at={(yticklabel cs:1.3)}, rotate=0,anchor =center},
bar width=4pt,
]
\addplot table [
y=Y-Position,
x=meas1,
] {data1.dat};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
title = {Meas. 2},
yticklabel pos=right,
yticklabels from table={data1.dat}{winner},
ylabel = {Winner},
every axis y label/.style = {at={(yticklabel cs:0.94)}, rotate=0,anchor =center},
ytick pos = right,
bar width=4pt,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data1.dat};
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
South Carolina
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data2.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas1,
] {data2.dat};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=right,
yticklabels from table={data2.dat}{winner},
ytick pos = right,
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data2.dat};
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Norway
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data3.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas1,
] {data3.dat};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=right,
yticklabels from table={data3.dat}{winner},
ytick pos = right,
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data3.dat};
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
答案1
- 您可以使用
extra description/.append code={\node at (yticklabel cs:0.5) [anchor=west] {<Region name>};}
将节点放置在轴的中间位置,即刻度标签的左侧。但是,在这种情况下,我会采用不同的方法,因为您要将多个东西相互对齐。 - 您可以将所有的
axis
环境放在同一个环境中tikzpicture
,并使用\matrix{<first axis> & <second axis> \\ <third axis> & <fourth axis>\\};
将图表排列在网格中。这将自动对齐实际轴区域,忽略刻度标签和标签。 - 如果使用
xlabel
选项而不是title
排版 x 轴标签的选项,则可以使用 命名该节点xlabel style={name=xlabel}
,并使用 命名 y 刻度标签yticklabel style={name=yticklabel}
。然后,您可以使用 对齐 y 轴标签ylabel style={at=(yticklabel.east|-xlabel.base), anchor=base east}
。这会将 y 轴标签放置在 y 刻度标签的水平位置和 x 轴标签底部的垂直位置。 - 您可以使用密钥
yticklabel shift=<length>
来实现这一点。 - 这可以像我们对齐 y 轴标签的方式一样完成。我建议您使用 对齐区域名称
extra description/.append code={\node (region label) at ({yticklabel cs:0.5}-|ylabel.west) [anchor=east,xshift=-1ex] {<region name>};}
。这样,1ex
无论该标签有多宽,它都会被放置在 y 轴标签的左侧。之后,您可以使用 对齐区域标题extra description/.append code={\node at (region label.east|-xlabel.base) [anchor=base east] {<region title>};}
。 \begin{figure}\begin{tikzpicture}\matrix{ <axis> & <axis> ...};\end{tikzpicture}\caption{<caption>}\end{figure}
\matrix[column sep=<length>, row sep=<length>]{<axes>};
\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{
compat=newest,
width=4cm,
height=4cm,
title style = {yshift = 10pt,name=title},
xtick align = inside,
xtick pos = both,
xticklabel pos = upper,
yticklabel style={name=yticklabel},
xlabel style={name=xlabel},
ylabel style={rotate=-90},
xmin=-5,
xmax = 140,
ytick align = outside,
ytick pos = left,
yticklabel pos=left,
ymin =0,
ymax = 5,
ytick=data,
xbar,
nodes near coords,
every node near coord/.append style = {anchor=west},
yticklabel shift=0.25cm,
region/.style={
extra description/.append code={\node (region label) at ({yticklabel cs:0.5}-|ylabel.west) [anchor=east,xshift=-1ex] {#1};}
},
region title/.style={
extra description/.append code={\node at (region label.east|-xlabel.base) [anchor=base east] {#1};}
}
}
% Measurements contry 1
\begin{filecontents}{data1.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 15 45 John
30-40 2 20 13 Al
40-50 3 12 4 Andrew
50-60 4 24 1 Tom
\end{filecontents}
% Measurements contry 2
\begin{filecontents}{data2.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 44 30 Peter
30-40 2 15 33 Steve
40-50 3 2 48 David
50-60 4 66 98 Alister
\end{filecontents}
% Measurements contry 3
\begin{filecontents}{data3.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 13 22 Arne
30-40 2 1 48 Per
40-50 3 2 4 Ola
50-60 4 33 61 Anders
\end{filecontents}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\begin{tikzpicture}
\matrix[column sep=4pt, row sep=2pt]{
\begin{axis}[
xlabel = {Meas. 1},
yticklabel pos=left,
yticklabels from table={data1.dat}{Age-interval},
ylabel = {Age interval},
ylabel style = {
at=(yticklabel.east|-xlabel.base),
anchor=base east,
name=ylabel
},
bar width=4pt,
region=Ohio,
region title=Region
]
\addplot table [
y=Y-Position,
x=meas1,
] {data1.dat};
\end{axis}
&
\begin{axis}[
xlabel = {Meas. 2},
yticklabel pos=right,
yticklabels from table={data1.dat}{winner},
ylabel = {Winner},
ylabel style = {
at={(yticklabel.west|-xlabel.base)},
anchor=base west,
},
ytick pos = right,
bar width=4pt,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data1.dat};
\end{axis}
\\
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data2.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
region=South Carolina
]
\addplot table [
y=Y-Position,
x=meas1,
] {data2.dat};
\end{axis}
&
\begin{axis}[
yticklabel pos=right,
yticklabels from table={data2.dat}{winner},
ytick pos = right,
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data2.dat};
\end{axis}
\\
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data3.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
region=Norway
]
\addplot table [
y=Y-Position,
x=meas1,
] {data3.dat};
\end{axis}
&
\begin{axis}[
yticklabel pos=right,
yticklabels from table={data3.dat}{winner},
ytick pos = right,
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data3.dat};
\end{axis}
\\};
\end{tikzpicture}
\caption{Results of some contest for men}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}