6

How is it possible to change the color of a boxplot from pgfplots? Black would be wounderful! Here is an MWE:

\documentclass{scrartcl}

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{tikzpicture}
\begin{axis}[y=1cm, try min ticks=2]
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
21\\
};
\end{axis}
\end{tikzpicture}

\end{document}
  • 2
    Add black to the \addplot options. Is that what you mean? – Jake Jan 31 '14 at 15:41
  • Thank you! An \addplot+[black, boxplot] works, whereas \addplot+[boxplot, black] causes an error. This was the reason of my question. – Dirk Jan 31 '14 at 17:05
  • Do you have an additional hint, how to draw the outliers black too? Currently, only the border is black, its inner is still blue. Thanks! – Dirk Jan 31 '14 at 17:36
6

To get a completely black box plot (including the marks for the outliers), the easiest thing is to use \addplot [mark=*, boxplot] (note the omission of the +). What the + does is indicate to PGFPlots that you want to use the style defined by the plot cycle list: by default, the first plot is blue, the second plot is red, and so on. That's not what you want in this case, however. By omitting the +, PGFPlots reverts to the basic drawing options: black lines, no markers. By adding mark=*, you're specifying what marker to use for the outliers.

\documentclass{scrartcl}

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{tikzpicture}
\begin{axis}[y=1cm, try min ticks=2]
\addplot [mark=*, boxplot]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
21\\
};
\end{axis}
\end{tikzpicture}

\end{document}
2

Just in case someone was (like me) wondering how to set fill and border color separately for a boxplot, here's an example that does this (borrowed from my blog):

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
% Nice color sets, see see http://colorbrewer2.org/ 
\usepgfplotslibrary{colorbrewer}
% initialize Set1-4 from colorbrewer (we're comparing 4 classes),
\pgfplotsset{compat = 1.15, 
             cycle list/Set1-8} 
% Tikz is loaded automatically by pgfplots
\usetikzlibrary{pgfplots.statistics, pgfplots.colorbrewer} 
% provides \pgfplotstabletranspose
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
22, 26, 30, 17, 45
10, 15, 13, 12, 17
12, 30, 6,  57, 10
33, 38, 36, 25, 24
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \pgfplotstableread[col sep=comma]{data.csv}\csvdata
    % Boxplot groups columns, but we want rows
    \pgfplotstabletranspose\datatransposed{\csvdata} 
    \begin{axis}[
        boxplot/draw direction = y,
        x axis line style = {opacity=0},
        axis x line* = bottom,
        axis y line = left,
        enlarge y limits,
        ymajorgrids,
        xtick = {1, 2, 3, 4},
        xticklabel style = {align=center, font=\small, rotate=60},
        xticklabels = {Apples, Oranges, Bananas, Melons},
        xtick style = {draw=none}, % Hide tick line
        ylabel = {Juiciness},
        ytick = {20, 40}
    ]
        \foreach \n in {1,...,4} {
            \addplot+[boxplot, fill, draw=black] table[y index=\n] {\datatransposed};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

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.