# Plotting 2D Gaussian with fading in pgfplots

I would like to plot the intensity profile of a Gaussian mode in a 2D plane. This is given by I = exp(-2*(x^2+y^2)/w00^2), where x and y are distances from the central axis and w00 is a constant. I want the centre of the Gaussian (with the highest intensity) to be plotted in some specific color (ideally light orange or red) and this color should fade to white as the distance from the centre increases, in accordance with the Gaussian intensity. Below is a MWE that draws a circle and fades from red to white towards the edge of the circle. The standard fading seems to be linear, however.

In my case, the fading should occur in accordance with the Gaussian function, so slower variation in colour around the peak, followed by a sharper drop and then slowly level off to white at large distances. It should NOT just be linear. How do I achieve this with the pgfplots package?

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\fill[inner color=red, outer color=white] (0,0) circle (1);
\end{tikzpicture}
\end{document}

• Welcome to TeX.SX! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. – Stefan Pinnow Sep 20 '17 at 18:27
• I have edited the question and added a MWE – Quantum Sep 20 '17 at 22:26
• You can shade using a function. It is quit computationally intensive, so you need to bear the inefficiency of doing this in TeX in mind. – cfr Sep 21 '17 at 0:18
• See '109.2.3 General (Functional) Shadings' in the manual, but I think you would be better off using another tool. – cfr Sep 21 '17 at 0:54
• I think I have found another way of doing this: Producing a 3D surface plot of the desired function with pgfplots (where point meta corresponds to the z value, which is the same as the function value) and then setting view={0}{90}. I am not sure if this is the most efficient way since the plot is actually 3D and the view has to be computed in an additional step, but this works for me. – Quantum Sep 23 '17 at 19:44

Here's a attempt in Metapost that might inspire someone to show you how to do something in pgfplots. \RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
%\mplibtextextlabel{enable}
%\mplibnumbersystem{double}
\begin{mplibcode}
beginfig(1);

vardef exp(expr x) = mexp(256x) enddef; % hide MP's version of exp

vardef normal_pdf(expr x) =
exp(-1/2x*x)/2.50662827463  % \sqrt(2\pi) \simeq 2.50663
enddef;

numeric r, s, u, v;
u = 16mm; v = 16mm; r=4; s=1/256;

for t=-r step s until r:
y := normal_pdf(t);
drawdot (t*u, y*5v) withpen pencircle scaled 1/2
withcolor (2y)[white, blue];
endfor

% axes
draw (left--right) scaled (r*u);
draw (down--up) scaled 2;

endfig;
\end{mplibcode}
\end{document}


This is wrapped up in luamplib so you'll need to use lualatex to compile by example, or adapt it to ordinary Metapost.

The idea is that I'm using the value of the y coordinate to change the color, and drawing enough dots so that the line looks continuous. You could also use the y value to change the size of the dots.

• This is not quite what I wanted as this plots y as a function of x. I'm interested in a function of two variables f(x,y), however. My plot should be in the x-y plane, where the colour at each point should depend on the function value at that point. – Quantum Sep 23 '17 at 19:37
• You could use the same technique of drawing a dot at each (x,y) point and calculating the colour from your equation for I, with nested loops for x and y. – Thruston Sep 23 '17 at 22:44

You could just cheat. This of course does not work if you add axes and so on, which would reveal that the plot only gets whiter rather than fading away.

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines = none,colormap={fake}{
rgb255(0cm)=(255,255,255); rgb255(1cm)=(255,0,0)}]
z buffer=sort] {exp(-2*(x^2+y^2)/2^2)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[axis lines = none,colormap={fake}{
rgb255(0cm)=(255,255,255); rgb255(1cm)=(255,0,0)}] 