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.

Here is an image of the situation.

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


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


(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 '16 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 '16 at 14:45

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.



\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;

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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