Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

Full range of colorbar

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

bars with using just a part of the range

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,
  }]
\addplot [draw=none] coordinates {(0,0)};
\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}
\pgfdeclareverticalshading{someShading}{50bp}{
color(0bp)=(C1);
color(25bp)=(C2);
color(50bp)=(C3)
} 
\shade [shading=someShading](1,4) +(-.5,-3.5) rectangle ++(.5,3.5);
\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

share|improve this question

2 Answers 2

up vote 5 down vote accepted
\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}
\pgfdeclarehorizontalshading{someShading}{50bp}{
color(0bp)=(C1);
color(25bp)=(C2);
color(50bp)=(C3)
}
\clip (0,0) rectangle ++(6,2.5);
\shade [shading=someShading](0,0) rectangle ++(6,0.5);
\shade [shading=someShading](0,0.51) rectangle ++(7,0.5);
\shade [shading=someShading](0,1.01) rectangle ++(8,0.5);
\shade [shading=someShading](0,1.51) rectangle ++(9,0.5);
\shade [shading=someShading](0,2.01) rectangle ++(10,0.5);
\end{tikzpicture}

\end{document}

enter image description here

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,
  }]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
  \begin{scope}[yshift=1.51cm]
\begin{axis}[
  colorbar style={
    width=14cm,
  }]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
  \begin{scope}[yshift=1.01cm]
\begin{axis}[
  colorbar style={
    width=13cm,
  }]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
  \begin{scope}[yshift=0.51cm]
\begin{axis}[
  colorbar style={
    width=12cm,
  }]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
  \begin{scope}
\begin{axis}[
  colorbar style={
    width=10cm,
  }]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Thank you! That is actually pretty easy. –  Mandy Aug 23 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
    \pgfplotscolormaptoshadingspec{\pgfkeysvalueof{/pgfplots/colormap name}}{#2}\result
    % define and use a shading in pgf:
    \def\tempb{\pgfdeclarehorizontalshading{tempshading}{1cm}}%
    % 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);
        \pgftext[bottom,left]{\pgfuseshading{tempshading}}%
    \end{tikzpicture}%
}%

\pgfplotsset{colormap/jet}%

\colormapclipped{0}{8cm}

\colormapclipped{0.25}{8cm}

\colormapclipped{0.5}{8cm}

\colormapclipped{0.75}{8cm}

\end{document}

enter image description here

share|improve this answer
    
Thank you very much! –  Mandy Aug 23 at 17:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.