This small snippet adds the functionality to place a colorbar besides a TikZ node and hence an image.
In fact it's easier to employ pgfplots
capability of producing colorbars
and place the image in an axis
environment. Also, pgfplots
directly
supports viridis
, which is nice.
\NodeColorbar
adds a colorbar to the node specifyed by the key node
. This
node should be set anchor=south west
(up to now). The Colorbar is then placed
besides (left) to the node adopting its height.
node
name of the node, the colorbar is placed relative to; this argument is mandatoryposition
(0.05) position with respect to the right side of the node given in percent of the nodes width, i.e. the standard value leaves 5% between the nodes border and the center of the colorbarwidth
(0.025) complete width of the colorbar, i.e. width/2 to the left and right of the position determines the width (hence position-width/2 should be greater than 0)colorbar
provide your own colorbar manually (you also can add this to thecolors
key, see next), by specifying each color fading yourself with tuples of two: value/color, where the first value must be 0, the values should be increasing and the last one should be 1. The colors are just xcolorscolors
sets thecolorbar
key directly with predefined styles, which are up to nowgray
black (0) to white (1) colorbarhsv
the colormaphsv
as provided by MatLab, i.e. the hue range of the HSV color spacejet
thecolormap jet
provided by MatLabrwb
from red (0) to white (0.5) to blue (1)
y range
specify the range of the values the colorsbar indicates, given in the formmin to max
. Specifying this value activates the short form (seey ticks
below)y tick at border
(false) put ticks at the borders (0,1) of the colorbar. This value only has effect if the short form is activated (seey ticks
). If it isfalse
these is half a tick space left at each bordery ticks
There are two possibilities to specify ticks- short form. By specifying
y range
, this value should be an integer specifying the number of ticks to be used. Together withy tick at border
this determines the positions and values of the ticks. Each tick is printed using\pgfmathprintnumber
, see their documentation to change the appearance of numbers. - each tick is given as a tuple position/label, where position is relative,
i.e. a value in [0,1] and label is a text or math to be displayed at the
ticks end.
Example:
y ticks = {0/{\(pi\)}, 0.5/{\(0\)}, 1/{\(\pi\)}}
- short form. By specifying
Combines the \NodeColorbar
and provides an interface to directly specify the
image to be included. There are 3 Parameters, the first two are optional. The
parameters are
- the keys given to
\NodeColorbar
(see last section). - the keys given to
\includegraphics
such aswidth=
orheight=
and is optional similar to the one of the command itself. - the path to the image, cf. the second parameter of
\includegraphics
ICBFrame
styling of the frame surrounding the colorbarICBImage
in the\includegraphicsWithColorbar
, these keys are added to the node containing the imageICBLabel
styling for the labels at each tick.ICBTick
styling for the ticks
- The code was only tested using TikZ 3.0.0
- works up to now only if the node is set
anchor=south west
- in certain ranges (i.e. -0.5 to 0.5) there are rounding errors, when specifying an odd number of ticks for the value of 0.
- The Ticks-Type is up to now covering the right half of the colorbar. This could be individualized maybe by drawing the complete line and defining a dash-pattern
This small code snipped is published under the Beer/Coffee ware license as proposed by Poul-Henning Kamp.
% THE BEER-WARE LICENSE (Rev. 42):
% Ronny Bergmann <mail@rbergmann.info> wrote this file. As long as
% you retain this notice you can do whatever you want with this stuff. If we
% meet some day, and you think this stuff is worth it, you can buy me a beer
% or coffee in return.