我正在尝试用 pgfplots 绘制一个条形图,其中包含两个数据系列和两个独立的 y 轴(一个在左边,一个在右边)。
为了拥有不同的轴,我似乎需要将每个轴放在\addplot
自己的axis
环境中(参见下面的代码)。但是,当我这样做时,这些条形图会相互重叠,我无法将它们分开:
如果\addplot
命令相同axis
,则条形图会正确分组,但我无法使用第二个 y 轴。
这个问题看起来很有希望,但那里的所有情节都是在第一个axis
环境中绘制的
这是我的代码:
\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.5.1}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
axis y line*=left,
axis x line=none,
ymin=0, ymax=6,
ylabel=data set 1,
enlarge x limits=0.2,
xtick=data]
\addplot[mark=*,draw=black,fill=cyan,error bars/.cd, y dir=both, y explicit, error bar style={line width=1pt,solid, black}] coordinates {
((a),2.208) +- (0.004, 0.004)
((b),2.394) +- (0.028, 0.028)
((c), 2.452) +- (0.040, 0.040)
((d), 4.000) +- (0.757, 0.757)
((e), 2.561) +- (0.245, 0.245)
((f), 2.600) +- (0.423, 0.423)
((g), 2.731) +- (0.267, 0.267)
((h), 2.840) +- (0.207, 0.207)
((i), 2.000) +- (0.146, 0.146)
};
\end{axis} % start a new axis for the second data set
\begin{axis}[
ybar,
symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
axis y line*=right,
ymin=0, ymax=5,
ylabel=data set 2,
enlarge x limits=0.2]
\addplot[mark=*,fill=red!50,error bars/.cd, y dir=both, y explicit, error bar style={line width=1pt,solid, black}] coordinates {
((a),2.21) +- (0.004, 0.004)
((b),1.394) +- (0.028, 0.028)
((c), 3.452) +- (0.040, 0.040)
((d), 2.346) +- (0.757, 0.757)
((e), 1.561) +- (0.245, 0.245)
((f), 1.3300) +- (0.423, 0.423)
((g), 1.931) +- (0.267, 0.267)
((h), 2.820) +- (0.207, 0.207)
((i), 3.00) +- (0.146, 0.146)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
也许你已经明白了这一点,或者时间已经过去,所以你不需要它,但无论如何:使用适当的bar width
和bar shift
,以及xshift
标记的对应,你可以得到这个:
\documentclass{article} % don't use minimal
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.5.1}
\begin{document}
\begin{tikzpicture}[
declare function={
barW=8pt; % width of bars
barShift=barW/2; % bar shift
}
]
\begin{axis}[
ybar,
bar width=barW, % added
bar shift=-barShift, % added
symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
axis y line*=left,
axis x line=none,
ymin=0, ymax=6,
ylabel=data set 1,
enlarge x limits=0.1,
xtick=data
]
\addplot[mark=*,
mark options={xshift=-barShift}, % <-- added
draw=black,
fill=cyan,
error bars/.cd,
y dir=both,
y explicit,
error bar style={line width=1pt,solid, black}
] coordinates {
((a),2.208) +- (0.004, 0.004)
((b),2.394) +- (0.028, 0.028)
((c), 2.452) +- (0.040, 0.040)
((d), 4.000) +- (0.757, 0.757)
((e), 2.561) +- (0.245, 0.245)
((f), 2.600) +- (0.423, 0.423)
((g), 2.731) +- (0.267, 0.267)
((h), 2.840) +- (0.207, 0.207)
((i), 2.000) +- (0.146, 0.146)
};
\end{axis} % start a new axis for the second data set
\begin{axis}[
ybar,
bar width=barW,
bar shift=barShift,
symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
axis y line*=right,
ymin=0, ymax=5,
ylabel=data set 2,
enlarge x limits=0.1
]
\addplot[mark=*,
mark options={xshift=barShift}, % <-- added
fill=red!50,
error bars/.cd,
y dir=both,
y explicit,
error bar style={line width=1pt,solid, black}
] coordinates {
((a),2.21) +- (0.004, 0.004)
((b),1.394) +- (0.028, 0.028)
((c), 3.452) +- (0.040, 0.040)
((d), 2.346) +- (0.757, 0.757)
((e), 1.561) +- (0.245, 0.245)
((f), 1.3300) +- (0.423, 0.423)
((g), 1.931) +- (0.267, 0.267)
((h), 2.820) +- (0.207, 0.207)
((i), 3.00) +- (0.146, 0.146)
};
\end{axis}
\end{tikzpicture}
\end{document}