When creating a (bar) plot in pgfplots
, tex uses a default list of colors. What are the definitions of these colors?
pgfplots – How to Use Default Cycle List Color Definitions in pgfplots
bar chartcolorpgfplots
Related Solutions
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 use cycle list name
after ybar
, since ybar
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 the every node near coord
style.
To align the bars with the tick marks, set ybar=0pt, bar shift=0pt
.
\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, color=red, 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}
[
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=0pt,
bar shift=0pt,
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={
black,
font=\footnotesize,
/pgf/number format/precision=3,
anchor=180
},
cycle list name=customcol
]
\pgfplotsinvokeforeach{0:0.041,0.041:0.0693}{
\addplot +[restrict x to domain=#1] table {\data};
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
The OPs code gives the expected result (from the point of view from PGFPlots), but I think "the user" expects that "the last shown color" in the colorbar
should be the last defined color in the colormap
.
With OPs code that "last color" is indeed used in the colorbar
, but only at 1 (and above), so it is invisible. Luckily with PGFPlots v1.14 there where introduced many new features regarding colormap
s and colorbar
s so it is now very easy to create the colorbar "the user expects".
For more details please have a look at the comments in the code.
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
colormap={parula}{
rgb255=(53,42,135)
rgb255=(15,92,221)
rgb255=(18,125,216)
rgb255=(7,156,207)
rgb255=(21,177,180)
rgb255=(89,189,140)
rgb255=(165,190,107)
rgb255=(225,185,82)
rgb255=(252,206,46)
rgb255=(249,251,14)
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% use the following key--value to get the desired result
colormap access=piecewise const,
% (just to save (vertical) space in the answer)
colorbar horizontal,
% % -----
% % (please note that also the following key isn't needed any more
% % in this solution. But then it could be that your PDF viewer
% % returns a strange result. For example in SumatraPDF this most
% % probably is a bug that is reported already
% % <https://github.com/sumatrapdfreader/sumatrapdf/issues/595>)
% colorbar sampled,
% % also this key isn't needed when using
% % `colormap access=piecewise constant' and the number of "samples"
% % equals the number of given colors in the colormap
% colorbar style={
% samples=11,
% },
% % -----
% % if you need more or less colors than the colormap provides than
% % you can use the new keys provided
% % (see "Building Colormaps ased on other Colormaps" in
% % section 4.7.6 of the PGFPlots manual (v1.14) on page 198ff)
% colormap={my sampled version of viridis}{
% samples of colormap=(10 of viridis)
% },
% % -----
point meta min=0,
point meta max=1,
]
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
pgfplots
's default color cycle list is available in the pgfplots source code