TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created an intensity-plot (x and y axes are positions, the color of the point gives the intensity) in an external program (igor pro) and included the "bare" plot via the \addplot graphics command into my axis-environment. Now I would like to create a "color-scale"-legend for this plot. The plot is in greyscales, white being no intensity and black being maximum intensity, color changing lineary with intensity.
Unfortunately, I'm not allowed to upload images yet, so I cannot show you what it should look like, so I hope my description is sufficient.
Is there a way to create such a custom legend?
Thans in advance,

here is the plot as it should like:
enter image description here
Creating the axes and labels with pgfplots is no problem, but i also need the colorscale legend.

share|improve this question
I upvoted your question, you should now have enough rep to upload the image. – Jake Sep 4 '12 at 13:17
Welcome to LaTeX! As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again until you get more rep points. – percusse Sep 4 '12 at 13:25
Thank you both for "boosting" me, and also for the trick with the workaround :) – Janek Sep 4 '12 at 13:41
Should black correspond to 0 or 1? – Jake Sep 4 '12 at 13:47
Black should be 1. I didn't want to post an image of my unpublished work, so I quickly created a new one and obviously used the wrong scaling :) Sorry for that. – Janek Sep 4 '12 at 13:59
up vote 5 down vote accepted

You can activate the colormap simply by calling colormap in the axis options. The default range is 0 to 1, so you wouldn't even need to do adjust it in this case, but usually you would use point meta min=<lower>, point meta max=<upper>.

There are a number of predefined colormaps which are documented in the manual. There is a grayscale colormap which assigns black to the lowest and white to the highest values. In case you want a different mapping, you can create your own using a statement like

colormap={whiteblack}{gray(0cm)=(1); gray(1cm)=(0)}

You can customise the colorbar using the same options that apply to axis environments by passing the options to colorbar style.


    axis equal image,
    axis on top,
    point meta min=0, point meta max=1,
    colormap={whiteblack}{gray(0cm)=(1); gray(1cm)=(0)},
    colorbar style={
        at={(1.1,0)}, % Coordinate system relative to the main axis. (1,1) is upper right corner of main axis.
        anchor=south west,
        height=2/3*\pgfkeysvalueof{/pgfplots/parent axis height}, % Scale the colorbar relative to the main axis
        /pgf/number format/.cd, % Change the key directory to /pgf/number format
        fixed, fixed zerofill, precision=1,
        /tikz/.cd  % Change back to the normal key directory
\addplot graphics [xmin=-1, xmax=1, ymin=-1, ymax=1] {image};
share|improve this answer
That's perfect! Thank you so much! – Janek Sep 4 '12 at 14:09

Your Answer


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.