# How to fill area under plot using a color map?

I have one diagram showing a function f(x,y) like imshow does, and a second diagram that shows the probability distribution P(f). Find below an image of the situation. The code for the plot at the bottom is basically

\addplot[const plot,fill=black!20,very thin] file{hist.dat} \closedcycle;


To increase the visual connection between both images, I'd like to use the colormap from the first diagram in the second diagram: The currently gray area below the histogram plot should be filled with the colormap's gradient in x direction.

I tried to achieve that with stuff like fill=mapped color and different plot types like ycomb and ybar interval, but it seems that, out of the box, only mesh and scatter support colormaps, and both are unsuitable for my scenario.

EDIT: For completeness, here is my solution, derived from the accepted answer.

\def\defcolormapshading#1#2{%
\pgfplotscolormaptoshadingspec{#2}{50pt}\tempa%
\def\tempb{\pgfdeclarehorizontalshading{#1}{\paperheight}}%
\expandafter\tempb\expandafter{\tempa}%
\let\tempa\undefined\let\tempb\undefined%
}%


Put this in your preamble. Then, before the tikzpicture environment, say

\defcolormapshading{myshading}{mymap}


(replace "mymap" by the colormap name, and "myshading" by a descriptive name for the shading). Then give the key shading=myshading to \addplot. The use of \pgfplotscolormaptoshadingspec like in this snippet is detailed in the pgfplots manual.

-
I would like to use the jet colormap as defined in the pgfplots manual. Though any colormap that I tried (except for hot which is used in the example on p. 175) fails: "the colormap 'jet' is undefined! Maybe you misspelled it?". How did you get the rainbow out of it? – riddleculous Mar 2 at 19:18
for some reason it is working, when I first do \pgfplotsset{colormap#2} and then \pgfplotscolormaptoshadingspec{\pgfkeysvalueof{/pgfplots/colormap name}}{50pt}\tempa – riddleculous Mar 3 at 14:45

## 1 Answer

You could just define an appropriate shading using \pgfdeclarehorizontalshading and use that to fill your plot. The colour at position 25bp of the shading is the leftmost visible color, that at 75bp is the rightmost visible colour.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}

\begin{document}
\begin{tikzpicture}
\pgfdeclarehorizontalshading{stefan}{100bp}{
color(0bp)=(blue!50!black);
color(25bp)=(blue!50!black);
color(50bp)=(blue);
color(55bp)=(green);
color(60bp)=(yellow);
color(65bp)=(orange);
color(70bp)=(red);
color(75bp)=(red!50!black);
color(100bp)=(red!50!black)
}

\begin{axis}[enlargelimits=false,domain=-2.63:1.39,point meta=x]
\addplot [shading=stefan] coordinates {(-2.63,0.2) (-0.5,0.4) (-0.2,0.5) (-0.1,2.6) (0,0.8) (0.1,0.7) (0.2,0.8) (0.4,2.2) (0.5,0.4) (1.39,0.3)} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}


-
Thank you very much. With the pointer to shadings, I found \pgfplotscolormaptoshadingspec which does the job for me. The only weird thing is that I have to hand-adjust the second parameter to that command, it seems to depend on the plot width. – Stefan Majewsky Sep 9 '11 at 10:13
Yeah, I played around with that command first, too, but found that using \pgfdeclarehorizontalshading directly (\pgfplotscolormaptoshadingspec is just a wrapper for this function) is less comfortable in case you need fine control over the the shading, since the pgfplots color maps need to be specified in uniform steps. – Jake Sep 9 '11 at 10:17