12

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.

  • I do not have a solution, but I am just curious as to how you want to choose the bar to plot with a different colour. Are you looking for a function that takes a bin number and colour as parameters? – sudosensei Jun 2 '14 at 23:14
  • Good point! A function with number and parameter would be perfect. Or maybe I'll have to draw two diagrams on top of each other somehow (two addplot). But honestly I don't know. Maybe I wish too much. – Dr. Manuel Kuehner Jun 3 '14 at 4:19
  • In common graphs, I usually use two axis environments with the same specifications (the graphs are overlapping). But this task looks more complicated. – Malipivo Jun 3 '14 at 6:01
  • My thoughts exactly :) – Dr. Manuel Kuehner Jun 3 '14 at 14:09
  • I'm thinking of a different way: to do computations in Lua/externally and plot a common bar type graph. – Malipivo Jun 5 '14 at 11:11
4
+50

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}
  • Great - I will have a look at it on the weekend. I don't get it yet. – Dr. Manuel Kuehner Jun 5 '14 at 8:15
  • There is an extra } on line 41 of the code example. As a result, the document does not compile cleanly. – mvkorpel Jun 5 '14 at 10:59
  • 1
    Ok, edits after corrections ... still artefacts. Thank you. – Tarass Jun 5 '14 at 11:54

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.