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.
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:
zoomed comparison (upscaled resolution only to make differences visible):
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...
okular
shows it correctly. This is really strange...please use okular or adobe
;)colorbar scaled
is always shown correctly, I have no idea why.