# Barplot: alternating colors, bars side by side

I'd like to have a barplot with bars side-by-side and with alternating colors. It should look like the ouput of the code below, just with alternating bar colors. (Got the idea from here)

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[ybar interval, xmin=0, xmax=0.3,]
{(0,2) (0.1,1) (0.2,0.5) (0.3,0.7)};
\end{axis}
\end{tikzpicture}

\end{document}


In the manual I only see options for plots that are added as separate plots, but I don't know how to align them properly. And my search only finds solutions with separated bars that can have different colors. How can I accomplish the task to have bars with different colors besides each other in pdflatex?

-

You can use two \addplot commands, checking whether the current coordinate index is odd or even using the ifthen package:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{ifthen}

\begin{document}

0   2
0.1 1
0.2 0.5
0.3 0.7
}\datatable

\begin{tikzpicture}
\begin{axis}[ybar, bar width=0.1, bar shift=0pt, xmin=-0.1, xmax=0.4,]
\end{axis}
\end{tikzpicture}

\end{document}

-

A very small variant of Jake's answer, without the ifthen package, simply using \ifodd...\else...\fi instead:

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass[tikz]{standalone}

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

\begin{document}

0   2
0.1 1
0.2 0.5
0.3 0.7
}\datatable

\begin{tikzpicture}
\begin{axis}[ybar, bar width=0.1, bar shift=0pt, xmin=-0.1, xmax=0.4,]

Ah, yes! I thought the ifthen package provided the \ifodd command. Silly. –  Jake Jul 16 '14 at 17:04