# Use just a specific range of a colormap or shading in tikz

I'd like to create an image with tikz that uses just a part of a predefined colormap or shading. More precisley, if I have e.g. the jet-colormap

I like to create several rectangles that use the jet colormap, but not the full range but rather some part:

I tried different approaches with a colormap, e.g. directly via colorbars (example 1) and with shadings (exmaple 2). But i didn't came up with a solution.

example 1

\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\pgfplotsset{ticks=none}
\begin{axis}[
hide axis,
scale only axis,
height=0pt,
width=0pt,
colormap/jet,
colorbar horizontal,
colorbar style={
width=10cm,
}]
\end{axis}
\end{tikzpicture}
\end{document}


example 2

\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\definecolor{C1}{RGB}{19,128,67}
\definecolor{C2}{RGB}{255,255,255}
\definecolor{C3}{RGB}{154,0,79}
color(0bp)=(C1);
color(25bp)=(C2);
color(50bp)=(C3)
}
\end{tikzpicture}

\end{document}


I'd like to avoid creating data for the individual rectangles to control the range of the colormap. The 'best case' would be to be able to adress an actual interval, e.g. choose a value between [0,1] or to adress an percentage interval like [0%,100%].

Thanks a lot for any help,

M

-

\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\definecolor{C1}{RGB}{19,128,67}
\definecolor{C2}{RGB}{255,255,255}
\definecolor{C3}{RGB}{154,0,79}
color(0bp)=(C1);
color(25bp)=(C2);
color(50bp)=(C3)
}
\clip (0,0) rectangle ++(6,2.5);
\end{tikzpicture}

\end{document}


Same clipping technique with pgfplots

\documentclass[]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\pgfplotsset{ticks=none,
hide axis,
scale only axis,
height=0pt,
width=0pt,
colormap/jet,
colorbar horizontal}
\clip (0,-0.75) rectangle ++(9.9,2.45);
\begin{scope}[yshift=2.01cm]
\begin{axis}[
colorbar style={
width=15cm,
}]
\end{axis}
\end{scope}
\begin{scope}[yshift=1.51cm]
\begin{axis}[
colorbar style={
width=14cm,
}]
\end{axis}
\end{scope}
\begin{scope}[yshift=1.01cm]
\begin{axis}[
colorbar style={
width=13cm,
}]
\end{axis}
\end{scope}
\begin{scope}[yshift=0.51cm]
\begin{axis}[
colorbar style={
width=12cm,
}]
\end{axis}
\end{scope}
\begin{scope}
\begin{axis}[
colorbar style={
width=10cm,
}]
\end{axis}
\end{scope}
\end{tikzpicture}
\end{document}


-
Thank you! That is actually pretty easy. –  Mandy Aug 23 '14 at 17:27

You can transform a color map to a PGF shading by means of \pgfplotscolormaptoshadingspec. Afterwards you can use tikz techniques to clip the resulting shading such that it shows only the visible area:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\thispagestyle{empty}

% #1: left offset in [0,1]
% #2: width
\newcommand\colormapclipped[2]{%
% convert colormap -> \result
% define and use a shading in pgf:
% where \result' is inserted as last argument:
\expandafter\tempb\expandafter{\result}%
\begin{tikzpicture}[
% compensate for the clip by scaling the image:
xscale={1/(1-#1)}
]
% clip away #1 percent of the width:
\clip ({(#1)*#2},0cm) rectangle (#2,1cm);
\end{tikzpicture}%
}%

\pgfplotsset{colormap/jet}%

\colormapclipped{0}{8cm}

\colormapclipped{0.25}{8cm}

\colormapclipped{0.5}{8cm}

\colormapclipped{0.75}{8cm}

\end{document}
`

-
Thank you very much! –  Mandy Aug 23 '14 at 17:12