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.
nodename 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)colorbarprovide your own colorbar manually (you also can add this to thecolorskey, 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 xcolorscolorssets thecolorbarkey directly with predefined styles, which are up to nowgrayblack (0) to white (1) colorbarhsvthe colormaphsvas provided by MatLab, i.e. the hue range of the HSV color spacejetthecolormap jetprovided by MatLabrwbfrom red (0) to white (0.5) to blue (1)
y rangespecify the range of the values the colorsbar indicates, given in the formmin to max. Specifying this value activates the short form (seey ticksbelow)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 isfalsethese is half a tick space left at each bordery ticksThere 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 borderthis 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
\includegraphicssuch aswidth=orheight=and is optional similar to the one of the command itself. - the path to the image, cf. the second parameter of
\includegraphics
ICBFramestyling of the frame surrounding the colorbarICBImagein the\includegraphicsWithColorbar, these keys are added to the node containing the imageICBLabelstyling for the labels at each tick.ICBTickstyling 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.