[Tex/LaTex] Custom legend for intensity plot

legendpgfplots

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,
John

edit:
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.

Best Answer

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.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis equal image,
    enlargelimits=false,
    axis on top,
    colorbar,
    point meta min=0, point meta max=1,
    colormap={whiteblack}{gray(0cm)=(1); gray(1cm)=(0)},
    colorbar style={
        title=Intensity,
        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};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question