2

I am trying to use a variation of the mapped color for a prepared contour plot. The line color is correctly adjusted using draw color=mapped color!50!black, but I am unable to use the mapped color value for the grid of the colorbar. The example below produces black gridlines, but I would like them to be a darker shade of the color corresponding to the tick value.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.11}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[colorbar,colorbar style={grid,grid style={color=mapped color!50!black}}]
        \addplot[contour prepared,contour/draw color=mapped color!50!black]
            table {
                2 2 0.8
                0.857143 2 0.6
                1 1 0.6
                2 0.857143 0.6
                2.5 1 0.6
                2.66667 2 0.6
                0.571429 2 0.4
                0.666667 1 0.4
                1 0.666667 0.4
                2 0.571429 0.4
                3 0.8 0.4
                0.285714 2 0.2
                0.333333 1 0.2
                1 0.333333 0.2
                2 0.285714 0.2
                3 0.4 0.2
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

1 Answer 1

4

A grid will always have the same color, changing its colors depending on the current value is unsupported. But there is a different way.

My idea is that we could draw two colorbars on top of each other: one with the shading (i.e. yours as-is) and one with colorbar sampled line and a bar marker. The key idea is to modify colorbar/draw, a code-key which generates the entire colorbar when it is executed:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.11}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            colorbar,
            colorbar/draw/.append code={%
                % the /.append code means that colorbar/draw is
                % executed as usual -- and we execute the following
                % code as well, which draws a _second_ colorbar on top
                % of it:
                \pgfplotsset{
                    colorbar sampled line={
                        samples at={0,0.2,0.4,0.6,0.8},
                        scatter,
                        scatter/use mapped color={draw=mapped color!50!black},
                        only marks,
                        mark=-, 
                        mark size=\pgfkeysvalueof{/pgfplots/colorbar/width}/2,
                        line width=2pt,
                    },
                    %
                    % do not typeset labels twice:
                    hide axis,
                    %
                    % colorbar sampled line overwrites (resets)
                    % colorbar/draw.
                    %
                    % Execute it to draw the result:
                    colorbar/draw,
                }%
            },%
        ]
        \addplot[contour prepared,contour/draw color=mapped color!50!black]
            table {
                2 2 0.8
                0.857143 2 0.6
                1 1 0.6
                2 0.857143 0.6
                2.5 1 0.6
                2.66667 2 0.6
                0.571429 2 0.4
                0.666667 1 0.4
                1 0.666667 0.4
                2 0.571429 0.4
                3 0.8 0.4
                0.285714 2 0.2
                0.333333 1 0.2
                1 0.333333 0.2
                2 0.285714 0.2
                3 0.4 0.2
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

The graphics layers could be better, but it appears to be ok.


EDIT

You asked for improvement regarding layered graphics. The good news is: my initial solution would work out of the box by adding \pgfplotsset{set layers} - this would automatically sort layers. Unfortunately, there seems to be a bug in pgfplots which means that layered graphics break colorbars :-( I will take care of that bug.

That bug lead me to a workaround. This workaround relies on internal knowledge of the colorbar implementation: every (current) colorbar respects a key called /pgfplots/colorbar addplot. The purpose of that key is to visualize the colorbar as such, without its axis. If we customize that one, we can archieve the correct layering. Here is the solution:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.11}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            colorbar,
            colorbar style={%
                colorbar addplot/.add={}{%
                    \addplot[
                        samples at={0,0.2,0.4,0.6,0.8},
                        clip marker paths,
                        scatter,
                        point meta=y,
                        scatter/use mapped color={draw=mapped color!50!black},
                        only marks,
                        mark=-, 
                        mark size=\pgfkeysvalueof{/pgfplots/colorbar/width}/2,
                        line width=2pt,
                    ]
                        (0.5,x);
                },
            },
        ]
        \addplot[contour prepared,contour/draw color=mapped color!50!black]
            table {
                2 2 0.8
                0.857143 2 0.6
                1 1 0.6
                2 0.857143 0.6
                2.5 1 0.6
                2.66667 2 0.6
                0.571429 2 0.4
                0.666667 1 0.4
                1 0.666667 0.4
                2 0.571429 0.4
                3 0.8 0.4
                0.285714 2 0.2
                0.333333 1 0.2
                1 0.333333 0.2
                2 0.285714 0.2
                3 0.4 0.2
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

2
  • This is great, thanks a lot - didn't know about colorbar sampled line. As you mentioned, the layers aren't quite right - is there any way to place the axis lines above the markers or clip the markers of the second axis using the axis lines of the first?
    – mynoduesp
    Commented Oct 5, 2014 at 11:30
  • 1
    Perfect, thanks! I tried set layers but hadn't realised the output was caused by a bug. Once again, I'm very impressed by how much pgfplots is capable of. One final question: can it do my laundry and cooking for me?
    – mynoduesp
    Commented Oct 5, 2014 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .