想要绘制双轴图,但我对一些细节不太清楚 - 也许有人可以给我指出正确的方向?主要问题是两个条形图重叠!
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, filecontents}
\definecolor{color1}{RGB}{206,230,202}
\definecolor{color2}{RGB}{145,208,206}
\definecolor{color3}{RGB}{86,185,210}
\definecolor{color4}{RGB}{63,151,194}
\definecolor{color5}{RGB}{51,113,170}
\definecolor{color6}{RGB}{39,75,147}
\definecolor{color7}{RGB}{27,39,124}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every axis plot post/.style={/pgf/number format/fixed},
ybar = 5pt,
bar width=8pt,
x=1.2cm,
axis y line*=left,
ymin = 0.2,
ymax = 1,
axis on top,
xtick={Default, AS, BFA, BFC, LS, BFA-LS, Robust},
enlarge x limits=0.2,
symbolic x coords={Default, AS, BFA, BFC, LS, BFA-LS, Robust},
%restrict y to domain*=0:1200, % Cut values off at 14
visualization depends on=rawy\as\rawy, % Save the unclipped values
after end axis/.code={ % Draw line indicating break
\draw [ultra thick, white, decoration={snake, amplitude=1pt}, decorate] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
},
nodes near coords={%
\pgfmathprintnumber[precision=2]{\rawy}% Print unclipped values
},
every node near coord/.append style={rotate=90, anchor=west},
axis lines*=left,
clip=false,
ylabel={Dice Score},
xlabel={Average for Method},
tick label style={font=\footnotesize},
]
\addplot[fill=color5] coordinates {(Default, 0.4512103812) (AS, 0.631082628) (BFA, 0.6600940641) (BFC, 0.6531076378) (LS, 0.6362765231) (BFA-LS, 0.6547464862) (Robust, 0.69035633)};
\end{axis}
\begin{axis}[
every axis plot post/.style={/pgf/number format/fixed},
ybar = 5pt,
bar width=8pt,
x=1.2cm,
ymin = 0,
ymax = 10,
enlarge x limits=0.2,
ylabel={Change in \%},
symbolic x coords={Default, AS, BFA, BFC, LS, BFA-LS, Robust},
visualization depends on=rawy\as\rawy, % Save the unclipped values
nodes near coords={%
\pgfmathprintnumber[precision=2]{\rawy}% Print unclipped values
},
every node near coord/.append style={rotate=90, anchor=west},
axis y line*=right,
axis x line=none,
]
\addplot[fill=color5] coordinates {(Default, 0.3512103812) (AS, 0.631082628) (BFA, 0.6600940641) (BFC, 0.6531076378) (LS, 0.6362765231) (BFA-LS, 0.6547464862) (Robust, 0.69035633)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
要修复ylabel
第二个的位置,ylabel
只需compat=1.3
在序言中添加或更高(与此同时,这也已在问题下方的评论)。
这些条形图重叠,因为您有两个独立的axis
环境,因此这两个\addplot
命令彼此“不了解”。因此,这些条形图只是绘制在 xticks 的顶部。为了避免这种情况,您必须手动移动它们。
有关更多详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\definecolor{color5}{RGB}{51,113,170}
\pgfplotsset{
% use this `compat` level or higher to make use of the "advanced"
% label positioning (this brings the second ylabel to the right)
compat=1.3,
% (created a style for the common options)
my axis style/.style={
every axis plot post/.style={/pgf/number format/fixed},
ybar=5pt,
bar width=8pt,
x=1.2cm,
axis on top,
enlarge x limits=0.1,
symbolic x coords={Default, AS, BFA, BFC, LS, BFA-LS, Robust},
%restrict y to domain*=0:1200, % Cut values off at 14
visualization depends on=rawy\as\rawy, % Save the unclipped values
% after end axis/.code={ % Draw line indicating break
% \draw [ultra thick, white, decoration={snake, amplitude=1pt}, decorate] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
% },
nodes near coords={%
\pgfmathprintnumber[precision=2]{\rawy}% Print unclipped values
},
every node near coord/.append style={rotate=90, anchor=west},
tick label style={font=\footnotesize},
xtick distance=1,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
my axis style,
ymin=0.2,
ymax=1,
axis y line*=left,
xlabel={Average for Method},
ylabel={Dice Score},
% to avoid overlapping, move the "first" bars to the left ...
bar shift={-\pgfplotbarwidth/2},
]
\addplot [fill=color5] coordinates {
(Default, 0.4512103812)
(AS, 0.631082628)
(BFA, 0.6600940641)
(BFC, 0.6531076378)
(LS, 0.6362765231)
(BFA-LS, 0.6547464862)
(Robust, 0.69035633)
};
\end{axis}
\begin{axis}[
my axis style,
ymin=0,
ymax=10,
axis x line=none,
axis y line*=right,
ylabel={Change in \%},
% ... and the second bars to the right
bar shift={\pgfplotbarwidth/2},
]
\addplot [fill=color5] coordinates {
(Default, 0.3512103812)
(AS, 0.631082628)
(BFA, 0.6600940641)
(BFC, 0.6531076378)
(LS, 0.6362765231)
(BFA-LS, 0.6547464862)
(Robust, 0.69035633)
};
\end{axis}
\end{tikzpicture}
\end{document}