I am unable to use custom colors from cycle list name
with pgfplotsinvokeforeach
. Some pointers could be gauged from this post, which discusses execution of various steps in a plot function.
In the MWE below, I am trying to segregate the bar chart into two categories where the numeric values are greater than median and other as less than the median value.
I use the x
value range as input for pgfplotsinvokeforeach
and restrict x to domain
to #1
values
From page 88, section 4.6.6 of pgfplots manual
/pgfplots/cycle list name={<macro>}
…the currently active cycle list will be used if you write either
\addplot+[<keys>]
…; or if you don't use square brackets as in >\addplot[<explicit plot specication>]
The cycle list name
functionality does not function possibly because I am specifying restrict x to domain
parameter explicitly to \addplot
function.
Code:
\listfiles
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{xcolor}
\pgfplotsset{xticklabel={\tick},scaled x ticks=false}
\pgfplotsset{plot coordinates/math parser=false}
\definecolor{darkblue}{RGB}{0,34,68}
\definecolor{ochre}{RGB}{255,161,0}
\pgfplotscreateplotcyclelist{customcol}{%
{fill=darkblue, draw=black},
{fill=ochre, draw=black}
}
\begin{document}
\begin{center}
\pgfplotstableread[col sep=comma,header=true]{
number,label
0.031,A
0.038,B
0.043,C
0.046,D
0.056,E
0.063,F
}\data
\begin{tikzpicture}
\begin{axis}
[
cycle list name=customcol,
width=5in,
symbolic y coords={A,B,C,D,E,F},
tick label style={font=\footnotesize},
label style={font=\footnotesize},
axis x line*=bottom,
axis y line*=left,
yticklabel style={text width=3cm, align=right,inner sep=0pt, outer sep=0pt},
xbar=5pt,
bar width=4pt,
point meta=x*100,
xlabel=number,
ylabel=label,
y tick label style={text width=0.5cm,align=center},
xticklabel=\pgfmathparse{100*\tick}\pgfmathprintnumber{\pgfmathresult}\,\%,
xticklabel style={/pgf/number format/.cd,fixed,precision=2},
nodes near coords={\pgfmathprintnumber[fixed zerofill,precision=1]{\pgfplotspointmeta}$\%$},
every node near coord/.append style={
font=\footnotesize,
/pgf/number format/precision=3,
anchor=180
}
]
\pgfplotsinvokeforeach{0:0.041,0.041:0.0693}{
\addplot +[restrict x to domain=#1] table {\data};
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
Possible Solutions
The syntax below is not 100% correct but wanted to illustrate the thought process:
Is there a way to create a list such as {[partition1,color1],[partition2,color2]}
which is input to \pgfplotsinvokeforeach
and #1
to be resolved as
\addplot [color1] +[restrict x to domain=[partition1] table {\data};
Output:
Addendum
1)The node labels are of same color as that of individual bar, this could be unreadable with certain colors. Is there a way to enforce a common color to all node labels, say black.
2) The y tick label alignment is a bit off, for category 1 the tick labels are below the bars and for category 2 the tick labels are above the bars
Thanks a lot!
Best Answer
There are a couple of issues here:
cycle list name
is the right way to approach this, and the cycle list will be respected as long as you use\addplot +[...]
. However, you need to usecycle list name
afterybar
, sinceybar
sets its own list.Furthermore, you need to make sure to either put a
%
after the last entry in the cycle list, or to put the closing}
directly behind it (not on a new line).To get black labels, add
black
to theevery node near coord
style.To align the bars with the tick marks, set
ybar=0pt, bar shift=0pt
.