13

I have a huge data file and want to visualize the data using histograms. I would like to color a single columns differently than the rest. Here's an example from the documentation (Section 5.11.2 Histograms):

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.statistics}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar interval,
xticklabel=
\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
]
\addplot+[hist={bins=3}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{axis}
\end{tikzpicture}

\end{document}

I get:

enter image description here

Now I want manually decide which column to color differently like (done with Paint...):

enter image description here

Has anyone an idea? For normal plots one would just put two plots in one diagram. But here I do not know how to do it.


This is maybe related.

1 Answers1

4

One can certainly make macros to automatize the process :

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.statistics}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar interval,
xticklabel=
\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick
]
\addplot+[hist={bins=3}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{axis}
\end{tikzpicture}


\begin{tikzpicture}
\begin{axis}[ybar interval,
    xtick=data,
    xticklabel interval boundaries,
    ]
\addplot+[hist={bins=3}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};
\end{axis}

\begin{axis}[ybar interval,
    ticklabel style={opacity=0},
    hist/data min={1},
    hist/data max={4}
    ]

% 1-4 4-7 7-10

\addplot+[hist={bins=1,
    data filter/.code={%
    \pgfmathparse{#1<4 ? #1 : "nan"}}
    }
]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};

\addplot+[hist={bins=1,
    data filter/.code={%
    \pgfmathparse{and(#1>=4,#1<7) ? #1 : "nan"}},
    },
style={draw=blue,fill=red!35}
]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};

\pgfplotsset{cycle list shift=-2}
\addplot+[hist={bins=1,
    data filter/.code={%
    \pgfmathparse{#1>=7 ? #1 : "nan"}}
    }
]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
};

\end{axis}
\end{tikzpicture}

\end{document}
Tarass
  • 16,912