超越条形图框架的条形图

超越条形图框架的条形图

pgfplots我正在尝试使用以下代码创建条形图:

\documentclass[a4paper]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread[row sep=\\, col sep=&]{
    KS     & SBC & EBIC & EBICFD & AICC & FPE & RNML & RNMLFD & PD1 & PD2 & PD3 & ORCL \\
    2      & 0.8 & 0.9  & 0.9    & 0.6  & 0.6 & 0.6  & 0.9    & 1.2 & 1.6 & 0.6 & 0    \\
    3      & 1.9 & 1.9  & 2      & 1.4  & 1.1 & 1.6  & 1.9    & 2   & 3.2 & 0.5 & 0.1  \\
    15     & 13.5& 13.5 & 13.6   & 11.8 & 12  & 13.5 & 13.6   & 14  & 29.6& 3.9 & 0    \\
}\mydata
\begin{tikzpicture}% coordinates
\begin{axis}[
ybar = 3pt,
bar width = 10pt,
width = \textwidth,
height= .5\textwidth,
legend style={at={(0.01,0.99)}, anchor=north west},
symbolic x coords={2,3,15},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
ymin=0,ymax=31,
xlabel={Average dist. to the true mask},
ylabel={KS index},
]
\addplot table[x=KS,y=SBC]{\mydata};
\addplot table[x=KS,y=EBIC]{\mydata};
\addplot table[x=KS,y=EBICFD]{\mydata};
\addplot table[x=KS,y=AICC]{\mydata};
\addplot table[x=KS,y=FPE]{\mydata};
\addplot table[x=KS,y=RNML]{\mydata};
\addplot table[x=KS,y=RNMLFD]{\mydata};
\addplot table[x=KS,y=PD1]{\mydata};
\addplot table[x=KS,y=PD2]{\mydata};
\addplot table[x=KS,y=PD3]{\mydata};
\addplot table[x=KS,y=ORCL]{\mydata};
\legend{$\mathrm{SBC}$, $\mathrm{EBIC}$, $\mathrm{EBIC}_{FD}$, $\mathrm{AIC}_{C}$, $\mathrm{FPE}$, $\mathrm{RNML}$, $\mathrm{RNML}_{FD}$, $\mathrm{PD}_{1}$, $\mathrm{PD}_{2}$, $\mathrm{PD}_{3}$, $\mathrm{Oracle}$};
\end{axis}
\end{tikzpicture}
\end{document}

但是,它给了我以下图表: 条形图

是否有人知道如何修改代码以便正确地重新调整条形图的尺寸,并且条形图不会像这样重叠?

谢谢,

答案1

轴上可容纳的条形数显然取决于以下几个因素:

  • 条形的宽度
  • 栅栏之间的空隙
  • 轴的宽度

假设您想保持轴宽度不变,则需要调整bar width和条形之间的间隙( 的参数ybar)。如果您的图可以比类\textwidth的默认值更宽article(这是standalone使用的),您可以增加它,并根据需要调整其他参数。

为了确保杆不会超出轴,请使用enlarge x limits={abs=<length>}合适的长度。

您仍然会遇到重叠的问题nodes near coords,因此我减小了字体大小并旋转了它们。我还将图例分成两列,以避免重叠。

现在各组之间的区分已经不大了,如果你希望有更多区分,可以bar width稍微减少一些。

我在代码中添加了一些注释来表明我所做的更改。

代码输出

\documentclass[a4paper]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread[row sep=\\, col sep=&]{
    KS     & SBC & EBIC & EBICFD & AICC & FPE & RNML & RNMLFD & PD1 & PD2 & PD3 & ORCL \\
    2      & 0.8 & 0.9  & 0.9    & 0.6  & 0.6 & 0.6  & 0.9    & 1.2 & 1.6 & 0.6 & 0    \\
    3      & 1.9 & 1.9  & 2      & 1.4  & 1.1 & 1.6  & 1.9    & 2   & 3.2 & 0.5 & 0.1  \\
    15     & 13.5& 13.5 & 13.6   & 11.8 & 12  & 13.5 & 13.6   & 14  & 29.6& 3.9 & 0    \\
}\mydata
\begin{tikzpicture}% coordinates
\begin{axis}[
% removed =3pt, to reduce space between bars
ybar,
% reduced bar width
bar width = 6.5pt,
width = \textwidth,
height= .5\textwidth,
legend style={
  at={(0.01,0.99)},
  anchor=north west,
  % remove this if you don't want to align the legend entries toward the left
  cells={anchor=west}
},
symbolic x coords={2,3,15},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
% reduce font size and rotate numbers above bars, to avoid overlap
every node near coord/.append style={font=\tiny,anchor=south west,rotate=45},
ymin=0,
% increase ymax a bit
ymax=34,
xlabel={Average dist. to the true mask},
ylabel={KS index},
% extend x-axis by a suitable amount
enlarge x limits={abs=8*\pgfplotbarwidth},
% set legend in two columns
legend columns=2
]
\addplot table[x=KS,y=SBC]{\mydata};
\addplot table[x=KS,y=EBIC]{\mydata};
\addplot table[x=KS,y=EBICFD]{\mydata};
\addplot table[x=KS,y=AICC]{\mydata};
\addplot table[x=KS,y=FPE]{\mydata};
\addplot table[x=KS,y=RNML]{\mydata};
\addplot table[x=KS,y=RNMLFD]{\mydata};
\addplot table[x=KS,y=PD1]{\mydata};
\addplot table[x=KS,y=PD2]{\mydata};
\addplot table[x=KS,y=PD3]{\mydata};
\addplot table[x=KS,y=ORCL]{\mydata};

\legend{$\mathrm{SBC}$, $\mathrm{EBIC}$, $\mathrm{EBIC}_{FD}$, $\mathrm{AIC}_{C}$, $\mathrm{FPE}$, $\mathrm{RNML}$, $\mathrm{RNML}_{FD}$, $\mathrm{PD}_{1}$, $\mathrm{PD}_{2}$, $\mathrm{PD}_{3}$, $\mathrm{Oracle}$};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容