0

I'm currently in the progress of attempting to combine my custom colorbar with a 2x1 groupplot such that it is shared. I've come quite far with the help of this topic, but arrived at an impasse.

The problem is that for some reason (I've isolated it to the introduction of the colorbar sampled option) a second color bar is introduced for second plot, whereas I never request it. To clarify, what I'm trying to achieve is the single colorbar spanning both plot, i.e. the top one that is displaying in the figure below.

I've put together a minimal working example, that works with the custom colorbar, ensures the correct colorbar height and illustrates the problem and put it below this output figure which illustrates the problem:

result of minimal example

Is anyone able to direct me towards a solution/fix to the problem? Although the minimal example might not indicate it, I do need the piecewise constant colorbar.

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\newlength\fheight
\newlength\fwidth

\begin{document}
\scriptsize
\centering
\setlength\fheight{4cm} 
\setlength\fwidth{5cm}
\begin{tikzpicture}

\begin{groupplot}[%
group style={group size=1 by 2,
xlabels at = edge bottom},
colormap={mymap}{[1pt]
rgb=(0.267004,0.00487433,0.329415);
rgb=(0.28315,0.113698,0.434361);
rgb=(0.271534,0.2104,0.505038);
rgb=(0.239637,0.300176,0.540669);
rgb=(0.202221,0.381534,0.554096);
rgb=(0.169529,0.456548,0.558034);
rgb=(0.140966,0.528998,0.555761);
rgb=(0.119984,0.601236,0.54219);
rgb=(0.146361,0.672773,0.509112);
rgb=(0.24974,0.740687,0.44999);
rgb=(0.405343,0.8007,0.361742);
rgb=(0.593466,0.848243,0.244848);
rgb=(0.797749,0.881204,0.118069);
rgb=(0.993248,0.906157,0.143936)},
colorbar sampled,
colormap access=piecewise constant,
colorbar style={
samples=14,
},
group/xlabels at = edge bottom,
xlabel={$x$},
ylabel={$y$},
]

\nextgroupplot[
width=0.838\fwidth,
height=\fheight,
scale only axis,
xmajorgrids,
ymajorgrids,
colorbar,
every colorbar/.append style={height=
    2*\pgfkeysvalueof{/pgfplots/parent axis height}+
    \pgfkeysvalueof{/pgfplots/group/vertical sep}}]
]

\addplot [draw=none] coordinates {(0,0)};

\nextgroupplot[
width=0.838\fwidth,
height=\fheight,
scale only axis,
xmajorgrids,
ymajorgrids,
]

\addplot [draw=none] coordinates {(0,0)};

\end{groupplot}
\end{tikzpicture}%

\end{document}
  • Or did I simply encounter a flaw in pgfplots? Is the problem at least reproducible from the MWE? – Steyn W. Feb 2 '18 at 10:11
3

In a moment of inspiration I removed the every from the every colorbar key and it worked (I guess). Is this what you want?

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\newlength\fheight
\newlength\fwidth

\begin{document}
\scriptsize
\centering
\setlength\fheight{4cm} 
\setlength\fwidth{5cm}
\begin{tikzpicture}

\begin{groupplot}[%
group style={group size=1 by 2,
xlabels at = edge bottom},
colormap={mymap}{[1pt]
rgb=(0.267004,0.00487433,0.329415);
rgb=(0.28315,0.113698,0.434361);
rgb=(0.271534,0.2104,0.505038);
rgb=(0.239637,0.300176,0.540669);
rgb=(0.202221,0.381534,0.554096);
rgb=(0.169529,0.456548,0.558034);
rgb=(0.140966,0.528998,0.555761);
rgb=(0.119984,0.601236,0.54219);
rgb=(0.146361,0.672773,0.509112);
rgb=(0.24974,0.740687,0.44999);
rgb=(0.405343,0.8007,0.361742);
rgb=(0.593466,0.848243,0.244848);
rgb=(0.797749,0.881204,0.118069);
rgb=(0.993248,0.906157,0.143936)},
colorbar sampled,
colormap access=piecewise constant,
colorbar style={
samples=14,
},
group/xlabels at = edge bottom,
xlabel={$x$},
ylabel={$y$},
]

\nextgroupplot[
width=0.838\fwidth,
height=\fheight,
scale only axis,
xmajorgrids,
ymajorgrids,
colorbar,
% every colorbar/.append style={height=% <------ REMOVED every FROM HERE
colorbar/.append style={height=
    2*\pgfkeysvalueof{/pgfplots/parent axis height}+
    \pgfkeysvalueof{/pgfplots/group/vertical sep}}]
]

\addplot [draw=none] coordinates {(0,0)};

\nextgroupplot[
width=0.838\fwidth,
height=\fheight,
scale only axis,
xmajorgrids,
ymajorgrids,
]

\addplot [draw=none] coordinates {(0,0)};

\end{groupplot}
\end{tikzpicture}%

\end{document}
  • That's one moment of brilliance, and such a simple solution. What I was trying to achieve, however, is the single colorbar being shared by both plots, so one large bar like to top one in my figure. – Steyn W. Feb 3 '18 at 14:15
  • @SteynW. Oh, sorry, I misunderstood the question. I'll leave the answer here for further reference. But in a few days you can accept yours. – Phelype Oleinik Feb 3 '18 at 16:46
  • No problem, thanks for thinking along! Can say you did trigger my inspiration to think outside the box. – Steyn W. Feb 3 '18 at 16:53
1

I do not consider this a fix to the problem, but I did find a workaround by following suggestions in this topic; to use the

Color Bars Outside Of an Axis

option from the pgf manual. I am posting this because it does provide the desired result hoping it provides useful for someone else.

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\newlength\fheight
\newlength\fwidth

\begin{document}
\scriptsize
\centering
\setlength\fheight{4cm} 
\setlength\fwidth{5cm}
\begin{tikzpicture}

  \begin{groupplot}[%
    group style={group size=1 by 2,
    group name=scatter_combined,
    xlabels at = edge bottom},
    colormap={mymap}{[1pt]
    rgb=(0.267004,0.00487433,0.329415);
    rgb=(0.28315,0.113698,0.434361);
    rgb=(0.271534,0.2104,0.505038);
    rgb=(0.239637,0.300176,0.540669);
    rgb=(0.202221,0.381534,0.554096);
    rgb=(0.169529,0.456548,0.558034);
    rgb=(0.140966,0.528998,0.555761);
    rgb=(0.119984,0.601236,0.54219);
    rgb=(0.146361,0.672773,0.509112);
    rgb=(0.24974,0.740687,0.44999);
    rgb=(0.405343,0.8007,0.361742);
    rgb=(0.593466,0.848243,0.244848);
    rgb=(0.797749,0.881204,0.118069);
    rgb=(0.993248,0.906157,0.143936)},
    colorbar sampled,
    colormap access=piecewise constant,
    colorbar style={
    samples=14,
    },
    colorbar to name=sharedcolorbar,
    group/xlabels at = edge bottom,
    xlabel={$x$},
    ylabel={$y$},
    every colorbar/.append style={height=
        2*\pgfkeysvalueof{/pgfplots/parent axis height}+
        \pgfkeysvalueof{/pgfplots/group/vertical sep}},
    ]

    \nextgroupplot[
    width=0.838\fwidth,
    height=\fheight,
    scale only axis,
    xmajorgrids,
    ymajorgrids,
    ]

    \addplot [draw=none] coordinates {(0,0)};

    \nextgroupplot[
    width=0.838\fwidth,
    height=\fheight,
    scale only axis,
    xmajorgrids,
    ymajorgrids,
    ]

  \addplot [draw=none] coordinates {(0,0)};

  \end{groupplot}
  \node (fig6_Legend) at ($(scatter_combined c1r1.center)!0.5!(scatter_combined c1r2.center)+(0.6\fwidth,0)$){\ref{sharedcolorbar}};


\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.