1

I am trying to create a bar plot using pgfplots using the following code:

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

However, it gives me the following graph: Bar plot

Does anyone know how to modify the code in order to get the bar graph re-dimension properly, and the bars not overlapping like this?

Thanks,

1 Answer 1

2

The number of bars you can fit in an axis depends obviously on several things:

  • the width of the bars
  • the gap between the bars
  • the width of the axis

Assuming you want to keep the axis width as it is, you need to adjust the bar width and the gap between bars (the argument to ybar). If your plot can be wider than the default \textwidth of the article class (which is what standalone uses), you can increase it, and adjust the other parameters as you like.

To make sure bars doesn't go outside the axis, use enlarge x limits={abs=<length>} with a suitable length.

You still get problems with overlapping of the nodes near coords, so I reduced the fontsize and rotated them. I also made the legend break over two columns, to avoid overlapping the bars.

There isn't much separation between the groups now, if you prefer to have more, you can reduce the bar width slightly.

I added some comments in the code to indicate what I changed.

output of code

\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}
1
  • Your answer is what I was looking for. Thanks. Sep 10, 2017 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.