使MATLAB图消失在具有不同透明度值的单色2D高斯背后

时间:2017-11-15 17:27:38

标签: matlab matlab-figure

下面的MATLAB代码绘制了一个2D绿色高斯,它具有透明度的变量值,代表波函数。

该代码还绘制了一个红色图,表示激光脉冲。 (见附图)。

我希望在背景中绘制红色激光脉冲,这样它穿过2D高斯的那部分将是不可见的。

有某种方法可以达到这个目的吗?

The figure produced:

function forQ
    close all; clc
    figure
    xlim_min=-11;
    xlim_max=11;
    ylim_min=-11;
    ylim_max=11; 
    global size_of_wavefunction;  size_of_wavefunction=2.5;        
    global x_laser; x_laser=-10.5:0.01:10.5;
    global omega; omega=8;
    global laser_strength; laser_strength=10;

    draw_main_wave_function;    
    hold on       
    laser_x_offset=-1.2;
    hold on
    drawLaser(laser_x_offset);
    hold on
    draw_main_wave_function;            
    ylim([ylim_min ylim_max])
    xlim([xlim_min xlim_max])    
    axis square
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function drawLaser(x_offset)    
    global x_laser;    
    global omega;
    global laser_strength;        
    envelope_extinction=3;        
    y_laser=laser_strength*exp(-envelope_extinction*(x_laser/10).^2).*cos(omega*(x_laser/10));    
    y_laser_for_plot=-y_laser;
    x_laser_for_plot=x_laser+x_offset;    
    plot(x_laser_for_plot,y_laser_for_plot,'-r','LineWidth',3)        
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function draw_main_wave_function

    global size_of_wavefunction;

    pre_exp_factor=0.5*0.8;
    c1=size_of_wavefunction*0.3;
    in_exp_factor=c1;
    x_offset=0;
    y_offset=0;

    draw_wave_function(pre_exp_factor, in_exp_factor, x_offset, y_offset);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function draw_wave_function( pre_exp_factor,in_exp_factor,...
                             x_offset, y_offset )  

    x=-3:0.01:3;  
    y=-3:0.01:3;  
    if (y_offset~=0)
        x=-5:0.1:5;  
        y=-11:0.1:11;         
    end
    [X,Y]=meshgrid(x,y);


    w_function=pre_exp_factor*exp(-in_exp_factor*((X-x_offset).^2+...
                                      (Y-y_offset).^2));

    colorMap = [0 , 1, 0];
    colormap(colorMap);

    min_alpha = 0; % 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

end

1 个答案:

答案 0 :(得分:0)

使用带有plot3的3D绘图并手动设置对象的位置。

view(2)将3D图形视图设置为xy图(在代码中可能没有必要)。

x = 0:0.1:2*pi;
figure
hold all
y = x(32);
plot3(y,sin(y),0*y+1,'go','MarkerSize',22,'MarkerFaceColor','g')
plot3(x,sin(x),0*x,'r-','LineWidth',3)
view(2)