Search code examples
matlabmatlab-figure

Plotting a 2D gaussian in MATLAB in one color but varaying levels of transparency


I want to plot 2D representation of a Gaussian wave function in MATLAB. I want the 2D plot to be in one color (green), which gets transparent away from the center of the Gaussian.

When I use imagesc (like in the next code) I get a Gaussian over a black square (as shown in the figure below).

I do not want the black backgrount, and I want the Gaussian to be in one color, but gets transparent away from the center, so that I won't get the black square, only green circle (over a white background) where the circle gets transparent away from its center.

How can I do that ???

close all;clc

figure
xlim_min=-4;
xlim_max=4;
ylim_min=-4;
ylim_max=4;

ylim([ylim_min ylim_max])
xlim([xlim_min xlim_max])

x=-1:0.001:1;  
y=-1:0.001:1;  
[X,Y]=meshgrid(x,y);

c1=10;
c1_new=c1*0.3;
x_offset=0;
y_offset=0;

w_function=0.5*0.25*exp(-c1_new*((X+x_offset).^2+...
                                      (Y+y_offset).^2));

imagesc(x,y,w_function);

ylim([ylim_min ylim_max])
xlim([xlim_min xlim_max])

enter image description here


Solution

  • To obtain transparency, use the AlphaData and AlphaDataMapping properties of the image. In the following, I'm explicitly computing the transparency (alpha) as an affine function of the data values, in order to specify minimum and maximum transparency values; and I set AlphaDataMapping to none so those values are used without any modifications.

    So, replace your line

    imagesc(x,y,w_function);

    by

    min_alpha = .2; % desired minimum alpha
    max_alpha = 1; % desired maximum alpha
    alpha = min_alpha + (max_alpha-min_alpha)/max(w_function(:))*w_function; % compute alpha
    imagesc(x,y,w_function,'AlphaData',alpha,'AlphaDataMapping','none'); % image with alpha
    

    Note how the blue part is more transparent (smaller alpha) than the yellow part.

    enter image description here


    To have the color blend smoothly with the outer region, set a mininum alpha of 0 and define the function on a larger grid. You can also change the colormap if desired. Since the transparency already varies, you probably want constant color in the colormap.

    close all;clc
    
    figure
    xlim_min=-4;
    xlim_max=4;
    ylim_min=-4;
    ylim_max=4;
    
    ylim([ylim_min ylim_max])
    xlim([xlim_min xlim_max])
    
    x=-2:0.001:2; % large enough that the function approximately ...
    y=-2:0.001:2; % ... reaches 0 within this rectangle
    [X,Y]=meshgrid(x,y);
    
    c1=10;
    c1_new=c1*0.3;
    x_offset=0;
    y_offset=0;
    
    w_function=0.5*0.25*exp(-c1_new*((X+x_offset).^2+...
                                          (Y+y_offset).^2));
    
    min_alpha = 0; % desired minimum alpha: set to 0
    max_alpha = 1; % desired maximum alpha
    alpha = min_alpha + (max_alpha-min_alpha)/max(w_function(:))*w_function; % compute alpha
    imagesc(x,y,w_function,'AlphaData',alpha,'AlphaDataMapping','none'); % image with alpha
    cm = [0 .7 0]; % define colormap: single color, dark green
    colormap(cm) % apply colormap
    
    ylim([ylim_min ylim_max])
    xlim([xlim_min xlim_max])
    

    enter image description here