1

I'm using pgfplots in latex to create a colorbar next to some existing images which uses a custom colormap. The issues I'm facing are:

  1. xticklabels are getting inside the colorbar (see image below). When I use compat flag, my colorbar vanishes. Hence, I have commented it out.
  2. How do I get a title above the colorbar? The way I do it, it creates that space.
  3. Also can I remove the xticklines?

Please see the MWE below:

  \documentclass{article}
  \usepackage{pgfplots}
  \usepackage{tikz}
  %\pgfplotsset{compat=1.17}
  \usepgfplotslibrary{colormaps}
  
  \pgfplotsset{
colormap/plasma/.style={%
    /pgfplots/colormap={plasma}{%
      rgb=(0.050383, 0.029803, 0.527975)
      rgb=(0.186213, 0.018803, 0.587228)
      rgb=(0.287076, 0.010855, 0.627295)
      rgb=(0.381047, 0.001814, 0.653068)
      rgb=(0.471457, 0.005678, 0.659897)
      rgb=(0.557243, 0.047331, 0.643443)
      rgb=(0.636008, 0.112092, 0.605205)
      rgb=(0.706178, 0.178437, 0.553657)
      rgb=(0.768090, 0.244817, 0.498465)
      rgb=(0.823132, 0.311261, 0.444806)
      rgb=(0.872303, 0.378774, 0.393355)
      rgb=(0.915471, 0.448807, 0.342890)
      rgb=(0.951344, 0.522850, 0.292275)
      rgb=(0.977856, 0.602051, 0.241387)
      rgb=(0.992541, 0.687030, 0.192170)
      rgb=(0.992505, 0.777967, 0.152855)
      rgb=(0.974443, 0.874622, 0.144061)
      rgb=(0.940015, 0.975158, 0.131326)
    },
  },
}

\begin{document}

  \begin{figure*}
     \centering
     \begin{tabular}{cccc}
     \begin{subfigure}[b]{0.25\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
         \caption{Image A}
     \end{subfigure}
&     \hfill
     \begin{subfigure}[b]{0.25\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
         \caption{Image B}
     \end{subfigure}
&     \hfill
     \begin{subfigure}[b]{0.25\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
         \caption{Image C}
     \end{subfigure}
&
    \begin{tikzpicture}
     \begin{axis}[
        hide axis,
        scale only axis,
        height=0pt,
        width=0pt,
        colormap/plasma,
        colorbar horizontal,
        point meta min=0,
        point meta max=25,
        colorbar style={
            width=3cm,
            rotate=90,
            xtick={0,5,10,15,20,25},
            title=Colormap Title,
        }
      ]
        \addplot [draw=none] coordinates {(0,0) (1,1)};
      \end{axis}
     \end{tikzpicture}
\end{tabular}
        \caption{\textbf{Image}}
\end{figure*}

enter image description here

1
  • Welcome to tex.sx. Commented Apr 15, 2021 at 0:15

1 Answer 1

1

The issue happens because you are rotating a horizontal colorbar. If you already have a plot, you can use colorbar right instead of colorbar horizontal. Otherwise, you can stick to the existing code and change the xticklabel, xtick and title styles.

  1. Shift the xtick labels using, xticklabel style={xshift=10pt,yshift=5pt},
  2. Remove the tickmarks using, xtick style={draw=none},
  3. Shift the title - title style={xshift=2.5cm,yshift=3.2cm},

The output: enter image description here

Code:

 \documentclass{article}
  \usepackage{pgfplots}
  \usepackage{tikz}
  %\pgfplotsset{compat=1.17}
  \usepgfplotslibrary{colormaps}
  \usepackage{subcaption}
  
  \pgfplotsset{
colormap/plasma/.style={%
    /pgfplots/colormap={plasma}{%
      rgb=(0.050383, 0.029803, 0.527975)
      rgb=(0.186213, 0.018803, 0.587228)
      rgb=(0.287076, 0.010855, 0.627295)
      rgb=(0.381047, 0.001814, 0.653068)
      rgb=(0.471457, 0.005678, 0.659897)
      rgb=(0.557243, 0.047331, 0.643443)
      rgb=(0.636008, 0.112092, 0.605205)
      rgb=(0.706178, 0.178437, 0.553657)
      rgb=(0.768090, 0.244817, 0.498465)
      rgb=(0.823132, 0.311261, 0.444806)
      rgb=(0.872303, 0.378774, 0.393355)
      rgb=(0.915471, 0.448807, 0.342890)
      rgb=(0.951344, 0.522850, 0.292275)
      rgb=(0.977856, 0.602051, 0.241387)
      rgb=(0.992541, 0.687030, 0.192170)
      rgb=(0.992505, 0.777967, 0.152855)
      rgb=(0.974443, 0.874622, 0.144061)
      rgb=(0.940015, 0.975158, 0.131326)
    },
  },
}

\begin{document}

  \begin{figure*}
     \centering
     \begin{tabular}{cccc}
     \begin{subfigure}[b]{0.25\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
         \caption{Image A}
     \end{subfigure}
&     \hfill
     \begin{subfigure}[b]{0.25\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
         \caption{Image B}
     \end{subfigure}
&     \hfill
     \begin{subfigure}[b]{0.25\textwidth}
         \centering
         \includegraphics[width=\textwidth]{example-image}
         \caption{Image C}
     \end{subfigure}
&
    \begin{tikzpicture}[rotate=0]
     \begin{axis}[
        hide axis,
        scale only axis,
        height=0pt,
        width=0pt,
        colormap/plasma,
        colorbar horizontal,
        point meta min=0,
        point meta max=25,
        colorbar style={
            width=3cm,
            rotate=90,
            title=Colormap Title,
            xtick={0,5,10,15,20,25},
            xtick style={draw=none},
            xticklabel style={xshift=10pt,yshift=5pt},
            title style={xshift=2.5cm,yshift=3.2cm},
        }
      ]
        \addplot [draw=none] coordinates {(0,0) (1,1)};
      \end{axis}
     \end{tikzpicture}
\end{tabular}
        \caption{\textbf{Image}}
\end{figure*}
\end{document}
1
  • This is great and helpful. How can I change the height of colorbar to be equivalent to height of shown image? As well as how to bring colorbar closer to the images? Commented Apr 15, 2021 at 5:03

You must log in to answer this question.

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