[Tex/LaTex] How to set the color of an horizontal bar chart

bar chartcolorpatternpgfplots

I am trying to make a violet horizontal bar chart. So far I managed to make a violet outline with a blue filling, but when I try to get a violet filling Latex compilation fails.

Here is an example of what I'd want:

    \documentclass[10pt]{article}

    \usepackage{color} %include colors
    \usepackage{pgfplots}
    \usetikzlibrary{patterns}

    \begin{document}

    \begin{figure}
      \begin{tikzpicture}
        \begin{axis}
          [
          axis lines*=left,
          xbar,
          xlabel={Pourcentage d'activité},
          symbolic y coords={Énergies},
          ytick=data,
          xmin=0,
          xmax=50,
          nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
          nodes near coords align={horizontal}
          ]
          \addplot[draw=violet, pattern=horizontal lines light violet]
          coordinates{(33,Énergies)};
        \end{axis}
      \end{tikzpicture}
    \end{figure}

    \end{document}

However if I compile with:

$ latexmk -pdf -pdflatex=lualatex -outdir=tmp test.tex

I get the following error:

! Package pgf Error: Undefined pattern `horizontal lines light violet'.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.25     \end{axis}

While the error message seems clear, if I only change horizontal lines light violet for horizontal lines light blue, the code compiles and I get the following output:

Blue bars with a violet outline

Moreover, if I look at the manual for pgfplots, which I found here, there is a snippet of code about using this horizontal line pattern at page 81, and if I click on the blue in the snippet, it sends me to a list of colors page 191 of the same document that contains, among others, violet.

What I want to do: in a first time, use the color violet as a filling for my chart, and ideally I'd like to be able to define a custom color (e.g. \definecolor{myPurple}{RGB}{73,10,61}) and use it as a filling for the bar chart.

edit:

  • I am using Windows 10 pro 64bits with MikTex
  • MikTex version is 2.9.6420
  • latexmk version Latexmk, John Collins, 19 Jan. 2017. Version 4.52c
  • lualatex version This is LuaTeX, Version 1.0.4 (MiKTeX 2.9.6420 NEXT 64-bit)

Best Answer

You can define a new pattern similar to horizontal lines light blue, where you set the two colours of the pattern yourself.

(pgfplots loads xcolor, so no need to load color separately.)

output of code

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\definecolor{myPurple}{RGB}{73,10,61}

% based on code in pgflibrarypatterns.code.tex
% corresponding to horizontal lines light blue, with a different colour
\pgfdeclarepatterninherentlycolored{horizontal lines Purple} % define name of pattern here
{\pgfpointorigin}{\pgfpoint{100pt}{4pt}}
{\pgfpoint{100pt}{4pt}}
{
  \pgfsetfillcolor{myPurple} % set first colour
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{100pt}{2.5pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{myPurple!90} % set second colour
  \pgfpathrectangle{\pgfpoint{0pt}{2pt}}{\pgfpoint{100pt}{2.5pt}}
  \pgfusepath{fill}
}

\begin{document}

\begin{figure}
  \begin{tikzpicture}
    \begin{axis}
      [
      axis lines*=left,
      xbar,
      xlabel={Pourcentage d'activité},
      symbolic y coords={Énergies},
      ytick=data,
      xmin=0,
      xmax=50,
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
      nodes near coords align={horizontal}
      ]
      \addplot[pattern=horizontal lines Purple]
      coordinates{(33,Énergies)};
    \end{axis}
  \end{tikzpicture}
\end{figure}

\end{document}