I'm trying to plot 2D gaussian function using "imshow" in MATLAB. However I could not see any white points in image. I tried to change sampling values and limits of x and y values. In addition to this, I tried z.^2 and higher orders but nothing changed.Thank you for any help.What can be the problem? My code is below:

sigma = 10;
[X,Y] = meshgrid(-40:0.5:40,-40:0.5:40);
z     = (1/(2*pi*(sigma^2)))*exp(-((X.^2)+(Y.^2))/(2*(sigma^2)));
surf(X,Y,z);%shading interp

Using "surf" command

using "imshow" command


Why not to simply use either:



set(h, 'LineStyle','None')

Although if you really want to use imshow then provide a set of extra parameters to scale the plotting range. It's more computational and memory efficient.

imshow(z, [min(min(z)) max(max(z))])
  • $\begingroup$ It is good to learn that method. However I need to plot my image in grayscale. $\endgroup$ – toygan kılıç Feb 18 '15 at 8:52
  • $\begingroup$ colormap gray $\endgroup$ – jojek Feb 18 '15 at 8:56

Look at the scale on your 3d plot. The z axis (or intensity value when plotted as an image) has an order of magnitude 10^-3

Imshow on any double (decimal) number in matlab automatically scales the image as 0=black 1= white. With that in mind you plot is essentially a matrix of zeros, or blacks.

You can solve this in two ways

1.Using imagesc, scales an image to take up the full dynamic range


2.Emulate imagesc

Max_val= max(max(z));
scaledz = 1/(Max_val - Min_val) * (z - Min_val); %this gets our numbers between 0-1

code isn't tested I just wrote it from memory on my cell phone. It may not work exactly but it should be pretty close


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.