3

I would like to create a bar plot with the bars having different colors. My bar plot is the following:

enter image description here

I have read solutions to similar problems, but I could not plot what I want.

My code is the following:

\documentclass[a4paper, 12pt]{article}

\usepackage[paperwidth=5in, paperheight=3.2in]{geometry}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}
\geometry{left=0mm, right=3mm,top=6mm, bottom=3mm,}


\begin{document}


\definecolor{mygr}{HTML}{e6e6e6}


\begin{figure}[!t]
\centering{
\begin{tikzpicture}

\begin{axis}[/pgf/number format/1000 sep={},
width=3.8in,
height=1.8in,
at={(0.758in,0.981in)},
scale only axis,
bar shift auto,
clip=false,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
every x tick/.append style={black},
xmin=0,
xmax=5,
xtick={1,2,3,4},
ytick={0,500,1000, 2000},
xticklabels={\empty},
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
every y tick/.append style={black},
ymin=0,
ymax=2000,
ylabel={YYY},
axis background/.style={fill=white}]

\addplot[ybar, bar width=0.2, fill=mygr, draw=black] table[row sep=crcr] {%
1   500  \\ 
2   1000  \\ 
3   1200   \\ 
4   1800   \\ 
};

\node[below left, align=right, rotate=0]
at (rel axis cs:0.308,-0.001) {GREEN};
\node[below left, align=right, rotate=0]
at (rel axis cs:0.488,-0.001) {BLUE};
\node[below left, align=right, rotate=0]
at (rel axis cs:0.690,-0.001) {GREY};
\node[below left, align=right, rotate=0]
at (rel axis cs:0.868,-0.001) {RED};

\end{axis}

\end{tikzpicture}}
\end{figure}

\end{document}

Any help would be appreciated. Thanks in advance!!

  • \centering doesnt take arguments. anyway, have you tried \addplot separately? then you can specify the colours of the plots individually. – Troy May 19 '17 at 10:54
3

You can use an \addplot command for each bar. But then you have to set bar shift=0pt.

\documentclass[a4paper, 12pt]{article}
\usepackage[paperwidth=5in, paperheight=3.2in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\geometry{left=0mm, right=3mm,top=6mm, bottom=3mm,}

\definecolor{mygr}{HTML}{e6e6e6}
\begin{document}
\begin{figure}[!t]
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        /pgf/number format/1000 sep={},
        width=3.8in,
        height=1.8in,
        at={(0.758in,0.981in)},
        scale only axis,
        clip=false,
        separate axis lines,
        axis on top,
        xmin=0,
        xmax=5,
        xtick={1,2,3,4},
        x tick style={draw=none},
        xticklabels={GREEN,BLUE,GREY,RED},
        ytick={0,500,1000, 2000},
        ymin=0,
        ymax=2000,
        ylabel={YYY},
        every axis plot/.append style={
          ybar,
          bar width=.2,
          bar shift=0pt,
          fill
        }
      ]
      \addplot[green]coordinates {(1,500)};
      \addplot[blue]coordinates{(2,1000)};
      \addplot[mygr]coordinates{(3,1200)};
      \addplot[red]coordinates{(4,1800)};
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

Result

enter image description here

  • Thank you very much!! I had tried to use different \addplot, but the positions of the bars were not correct. The bar shift was the problem. – Greek Freak May 19 '17 at 13:28

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.