3

pgfplots offers really nice surface plots as vector graphics, but file size can easily blow up. it is probably quite common (at least for me) to include a bitmap, which was obtained beforehand by converting a pgfplots-pdf with convert (or sometimes preferably with pdftoppm, see comparison at the bottom of this post) to a png bitmap file.

The MWE code then looks like this, just a plot with an included example image and a colorbar:

\documentclass{standalone}
\usepackage{graphics,pgfplots}
\pgfplotsset{compat=newest}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
             colorbar,
             xmin=0,xmax=1,
             ymin=0,ymax=1,
             colormap/viridis,
             colormap={mymap}{samples of colormap=(10 of viridis)},
             colormap access=piecewise constant,
            ]

\addplot graphics [xmin=0, xmax=1, ymin=0, ymax=1] {example-image-a};   
\end{axis}
\end{tikzpicture}
\end{document}

The only special thing is the piecewise constant colormap access. Unfortunately, this kind of colorbar causes problems for almost all pdf viewers I know of. Adobe and Okular work fine, but some examples are the Master PDF Editor (left picture), where the colorbar is just left empty, or evince (right picture), where the colorbar is not piecewise constant and shows a completely wrong color gradient.

wrong colorbar 1 wrong colorbar 2

The latter problem (i.e., wrong colors in colorbar) happens for most of the viewers I know, e.g. also the pdf-viewer integrated in Firefox. Since it sometimes works, I am quite sure that this is just a viewer problem. But nevertheless, it is probably better to solve it until viewers are fixed. I want everyone to enjoy my beautiful works of art created with pgfplots. :)

Is there a possibility to include a graphic into the colorbar axis, e.g., with \addplot graphics? It may be just the example-image-b.

For the real solution, one might use the colorbar created by pgfplots, see the next piece of code and its output below. (a pdfcrop-run seems to be necessary on the output pdf before converting it to a bitmap format)

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
             colorbar,
             xmin=0,xmax=1,
             ymin=0,ymax=1,
             colormap/viridis,
             colormap={mymap}{samples of colormap=(10 of viridis)},
             colormap access=piecewise constant,
             colorbar style = {hide axis},
             hide axis
            ]
\end{axis}
\end{tikzpicture}
\end{document}

almost unrelated info

To highlight the implied problems of convert (i guess it is a problem of ghostscript used to render the pdf), I have converted the colorbar only to a png , on the left with pdftoppm -png -r 30 in.pdf out and on the right with convert -density 30 in.pdf out.png. convert has serious problems with color transitions (it stays the same if a larger dpi value is used). Especially steep transitions become very grainy.

original very small colorbars:

pdftoppm convert

zoomed comparison (upscaled resolution only to make differences visible):

upscaled comparison

With only 30 dpi, the height/width ratio is not the same for both tools, but more importantly, the convert version has some wrong transitions. Furthermore, file size is 153 bytes for pdftoppm in contrast to 2742 bytes for convert.

Edit

When reading the manual, I noticed that the colorbar sampled option looks correct in all my viewers, even given that it should look the same as the piecewise-constant colorbar. Very strange...

  • 1
    It seems that okular shows it correctly. This is really strange... – Rmano Jul 30 at 13:52
  • Yeah, still not really satisfying. Mabye, I should add a first page in each document saying please use okular or adobe ;) – Faekynn Jul 30 at 17:45
  • 1
    Yep... On the other hand thin lines antialiasing is really horrible on okular, and acceptable on evince... – Rmano Jul 30 at 21:04
  • 1
    @Rmano now I have found a simple solution. the colorbar scaled is always shown correctly, I have no idea why. – Faekynn Jul 31 at 13:45
2

Simple Answer

For me, the colorbar sampled looks the same as the piecewise-constant version. The only difference is that it is viewed correctly in the programs I tried so far. Code is as follows (pay attention that the number of samples must be 1 larger than the number of constant pieces before!)

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
             colorbar sampled,
             xmin=0,xmax=1,ymin=0,ymax=1,
             colormap/viridis,
            % colormap={mymap}{samples of colormap=(10 of viridis)},
            % colormap access=piecewise constant,
             colorbar style={samples=11}, %% 10 +1!
            ]

\addplot graphics [xmin=0, xmax=1, ymin=0, ymax=1] {example-image-a};   
\end{axis}
\end{tikzpicture}
\end{document}

And the output looks good:

enter image description here

Another version, more complicated

Reading the manual carefully enough, I have found a satisfying answer. Of course the colorbar is just an axis, and its settings can be modified. Section 4.9.13 is about colorbars.

This answer is just copied together from several pieces in the named section.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

%%% page 284 in 4.9.13 of manual -> definition of 'every colorbar' style
%%  not necessary!
\pgfplotsset{
colorbar style={at={(parent axis.right of north east)},anchor=north west,},
colorbar shift/.style={xshift=0.3cm},
}
%%% modifying the draw-code allows for custom things inside the colorbar    
\pgfplotsset{
colorbar/draw/.code={
\begin{axis} [every colorbar,]% first comes the "original" colorbar code
\node at (0.5,0.5) {\includegraphics[width=0.5cm,height=7cm]{example-image-b}};  
% this assumes standard settings of x = [0,1], y = [0,1], colorbar width =0.5cm and height=7cm

%\addplot graphics [xmin=0, xmax=1, ymin=0, ymax=1] {example-image-a};   
% addplot graphics always draws the real colorbar and not the given bitmap.
\end{axis}
},
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
             colorbar,
             xmin=0,xmax=1,ymin=0,ymax=1,
             colormap/viridis,
             colormap={mymap}{samples of colormap=(10 of viridis)},
             colormap access=piecewise constant,
            ]

\addplot graphics [xmin=0, xmax=1, ymin=0, ymax=1] {example-image-a};   
\end{axis}
\end{tikzpicture}
\end{document}

I do not know if this answer or Torbjørn's is better. Both seem to require some manual tuning of the colorbar width, heigth, and x/y min/max values etc. as far as I can see it.

The output looks OK, as it does for the other answers.

enter image description here

1

Can't you just make a new (normal) axis and repeat the use of \addplot graphics? A groupplot makes aligning them easy. You just need to make sure that ymin/ymax is set correctly.

(The whitespace between colorbar and axis is from colorbar.pdf, so better cropping should fix that.)

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots} % loads tikz which loads graphicx
\pgfplotsset{compat=1.16} % it's recommended to set an explicit version
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
             group style={
                group size=2 by 1,
                horizontal sep=5mm
             },
             height=5cm,
             width=7cm,
             scale only axis,
             xmin=0,xmax=1,
             ymin=0,ymax=1,
            ]

\nextgroupplot
\addplot graphics [xmin=0, xmax=1, ymin=0, ymax=1] {example-image-a};

\nextgroupplot[width=1cm,xtick=\empty,ytick pos=right]
\addplot graphics[xmin=0,xmax=1,ymin=0,ymax=1] {colorbar};

\end{groupplot}
\end{tikzpicture}
\end{document}
  • 1
    This is a good solution. However, reading in the manual a bit, I just found out how to modify the axis-settings of the colorbar. I will have an own answer ready quite soon... – Faekynn Jul 31 at 11:16
  • now I found an even simpler solution with colorbar sampled – Faekynn Jul 31 at 11:52

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.