3

Is it possible for one colorbar to have two different scales? Like mentioned in this question: Matlab colorbar with two axes?

I'm trying to include two heatmaps with a colorbar in the center. As both heatmaps do have a different unit and scale, I'm in need of a solution to combine both in one colorbar.

A MWE for two plots with two colorbars is shown below. My goal is to combine them into one colorbar:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      group style = {
        group size = 2 by 1,
        horizontal sep = 5cm,
      },
      view = {0}{90},
    ]
      \nextgroupplot[colorbar right, colorbar style = {ylabel = A}]
      \addplot3[surf] {x^2 * y^2};
      \nextgroupplot[colorbar left, colorbar style = {ylabel = B}]
      \addplot3[surf] {sin(x) * sin(y)};
  \end{groupplot}
\end{tikzpicture}
\end{document}

Result of MWE

  • 1
    Hi, welcome. Do you have the code for the plots with two colorbars? Can you add that to your post in the form of a minimal working example (MWE)? – Torbjørn T. Aug 29 '17 at 14:03
  • Done. Thought that it wouldn't help that much. But I see now how it can be helpful. Sorry. – H. Müller Aug 30 '17 at 8:26
4

If a silly hack is fine, you could try something like this. I basically just position both colorbars right next to each other, and make each a bit narrower. The end result looks like one bar, but is two.

output of code

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      group style = {
        group size = 2 by 1,
        horizontal sep = 4cm,
      },
      view = {0}{90},
      % just for example (faster compile times ...)
      samples=10
    ]
      \nextgroupplot[
          % want tick labels on the left side
          colorbar left,
          colorbar style = {
            ylabel = A,
            % position it to the right of the axis
            at={(rel axis cs: 1.3,0)},
            anchor=south,
            % use this name to position second colorbar
            name=cb1,
            % reduce width
            width=2mm,
            % remove frame around colorbar
            axis line style={draw=none}
          }]
      \addplot3[surf] {x^2 * y^2};
      \nextgroupplot[
           % want tick labels on right side of colorbar
           colorbar right,
           colorbar style = {
             % give this as well a name, used to draw the frame later
             name=cb2,
             ylabel = B,
             % place it by the first colorbar
             at=(cb1.south),
             anchor=south,
             % needs a small adjustment
             xshift=-1mm,
             % set width
             width=2mm,
             % remove frame
             axis line style={draw=none},
             % the purpose of this was to highlight which numbers the scale label belongs to
             % but perhaps not needed with the next line
             yticklabel style={blue},
             % move scale label to right side
             every y tick scale label/.style={
                at={(rel axis cs:1,1)},
                anchor=south west
             }
           }]
      \addplot3[surf] {sin(x) * sin(y)};
  \end{groupplot}

% draw frame around both bars
\draw (cb1.south west) rectangle (cb2.north east);
\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.