0

I have some problems with the histogram. I have to:

  1. the axis x is double labeled and I should live only one label for each bar.

  2. each bar has to have a different color (red, blue, green, purple).

the code and the result are the following.

\begin{tikzpicture}
\begin{axis} [
symbolic x coords={$400$,$500$,$600$,$700$},
ymajorgrids=true,
ymin=0,
width=9cm, height=6.5cm
]
\addplot [ybar, bar width=.8cm, fill=blue, draw=black] coordinates
{($400$, 0.044) ($500$, 0.044) ($600$, 0.044) ($700$, 0.100)};
\end{axis}
\end{tikzpicture}

enter image description here

  • 1
    I don't know why it makes a double label, but you can easily avoid it since you use numbers for xlabels by using xtick=data instead of symbolic x coords. – Joe H. Aug 6 '15 at 2:54
2

Credits of this go to Jake.

% arara: pdflatex

\documentclass{article}   
\usepackage{pgfplots}
\pgfplotsset{%
    ,compat=1.12
    ,select row/.style={%
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
        }
    }

\usepackage{pgfplotstable}
\pgfplotstableread[header=false]{
    400 0.044
    500 0.044
    600 0.044
    700 0.100
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis} [
    xtick={0,...,4},
    xticklabels from table={\datatable}{0},
ymajorgrids=true,
ymin=0,
width=9cm, height=6.5cm,
ybar,bar width=.8cm,bar shift=0pt
]
\pgfplotsinvokeforeach{0,...,4}{
    \addplot+[draw=black] table [select row=#1, x expr=#1] {\datatable};
}
\end{axis}
\end{tikzpicture}   
\end{document}

enter image description here

  • great!! but I receive the following error: ! Package pgfkeys Error: Choice '1.12' unknown in choice key '/pgfplots/compat/ anchors'. I am going to ignore this key. – Stefano Aug 6 '15 at 10:56
  • Set compat= to the version of pgfplots you are using. Or update your system to get version 1.12. – LaRiFaRi Aug 6 '15 at 11:23
  • How are the bar colors controlled in this example? – lanoxx Jul 23 '17 at 10:08
  • Adding ,fill=red after draw=black in the \addplot option did it for me. – lanoxx Jul 23 '17 at 10:13

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.