1

Currently working on a barplot, where I have 6 series of bars to be plotted, with 4 discrete x-values. The issue I am now running into is that, as you can see below, for example for value4 there is considerable whitespace within the bars for one x-value as only 3 series have values for this specific x-value.

Is anyone aware of a way to remove the empty bars, and hence have the bars cluster around the x-value with the only major whitespace between the individual x-value bar-clusters?

Would greatly appreciate any ideas :)

\documentclass[crop,tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{xcolor}

% Define key color for the plot
\definecolor{col1}{HTML}{E63462}
\definecolor{col2}{HTML}{1E96FC}
\definecolor{col3}{HTML}{FCF300}
\definecolor{col4}{HTML}{084887}
\definecolor{col5}{HTML}{F58A07}
\definecolor{col6}{HTML}{414073}

\begin{document}

\begin{tikzpicture}
    \pgfplotsset{
        width=18cm,
        height=7cm,
    }
    \begin{axis}[
                % y-label settings
                ytick = {0, 5, 10, 15, 20},
                ylabel=Time per Bubble (s),
                ymin = 0,
                ymax = 20,
                ymajorgrids=true,
                % x-label settings
                x tick label style={
                    /pgf/number format/1000 sep=},
                symbolic x coords={Val1,Val2,Val3,Val4},
                xtick=data,
                % general settings
                ybar,
                scale only axis,
                grid style=dashed,
                bar width=16pt,
                enlarge x limits=0.15,
                axis lines*=left,
                nodes near coords,
                % Legend settings
                legend style={at={(0.5,1.1)},
                    anchor=north,
                    legend columns=6,
                    column sep=0.3cm,
                    draw=none
                },
                ]

                % Series 1
                \addplot[col1,fill=col1,text=black] coordinates {(Val1, 12) (Val2, 10.6) (Val3, 9.5) (Val4, 16)};
                % Series 2
                \addplot[col2,fill=col2,text=black] coordinates {(Val1, 10.6) (Val2, 9.5) (Val3, 8.5)};     
                % Series 3
                \addplot[col3,fill=col3,text=black] coordinates {(Val1, 12.4) (Val3, 9.8)};
                % Series 4
                \addplot[col4,fill=col4,text=black] coordinates {(Val1, 11.2) (Val2, 10) (Val3, 15.5)};
                % Series 5
                \addplot[col5,fill=col5,text=black] coordinates {(Val1, 11.5) (Val2, 11)};
                % Series 6
                \addplot[col6,fill=col6,text=black] coordinates {(Val1, 10.8) (Val3, 10) (Val4, 14.8)};

                % Legend
                \legend{Series 1, Series 2, Series 3, Series 4, Series 5, Series 6}
    \end{axis}
\end{tikzpicture}

\end{document}

At the example of the 4th x-value, what I would like to do is:

Bar plot with whitespace inbetween

1
  • One \addplot can not know how many points there are in an other \addplot. One way to archive this is not to use symbolic x coords. Set the x coordinate to what bar position you want for that bar. Commented Nov 11 at 22:59

1 Answer 1

1

Probably need to set coordinate manually according to 4 major x ticks then set the 4 x ticks to be {Val1,Val2,Val3,Val4}. Here is an example:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

% Define key color for the plot
\definecolor{col1}{HTML}{E63462}
\definecolor{col2}{HTML}{1E96FC}
\definecolor{col3}{HTML}{FCF300}
\definecolor{col4}{HTML}{084887}
\definecolor{col5}{HTML}{F58A07}
\definecolor{col6}{HTML}{414073}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=18cm,
height=7cm,
% y-label settings
ytick = {0, 5, 10, 15, 20},
ylabel=Time per Bubble (s),
ymin = 0,
ymax = 20,
ymajorgrids=true,
% x-label settings
xtick={3.5,9.5,15,19.5},
xticklabels={Val1,Val2,Val3,Val4},
% general settings
ybar=-16pt,
%scale only axis,
grid style=dashed,
bar width=16pt,
enlarge x limits={abs=16pt},
axis lines*=left,
nodes near coords,
% Legend settings
legend style={at={(0.5,1.1)},
    anchor=north,
    legend columns=6,
    column sep=0.3cm,
    draw=none
},
]
% Series 1
\addplot[col1,fill=col1,text=black] coordinates {(1, 12) (8, 10.6) (13, 9.5) (19, 16)};
% Series 2
\addplot[col2,fill=col2,text=black] coordinates {(2, 10.6) (9, 9.5) (14, 8.5)};     
% Series 3
\addplot[col3,fill=col3,text=black] coordinates {(3, 12.4) (15, 9.8)};
% Series 4
\addplot[col4,fill=col4,text=black] coordinates {(4, 11.2) (10, 10) (16, 15.5)};
% Series 5
\addplot[col5,fill=col5,text=black] coordinates {(5, 11.5) (11, 11)};
% Series 6
\addplot[col6,fill=col6,text=black] coordinates {(6, 10.8) (17, 10) (20, 14.8)};
% Legend
\legend{Series 1, Series 2, Series 3, Series 4, Series 5, Series 6}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .