Gaussian heat map [duplicate]
I'm trying to plot a Gaussian heat map peak, similar to the image, but when creating a normal random matrix, I'm not getting this sort of heat map.
Here's my code, the result is...
Any help is great!
import numpy as np
import pylab as plt
xy = np.random.normal(loc=256, scale= 200, size = (512,512))
print(xy)
plt.imshow(xy)
plt.show()
Solution 1:
np.random.normal
doesn't work the way you think it does.
This function returns random samples from the normal distribution defined by the loc
and scale
parameters. Each sample is independent of each other and hence the image is just noise.
Basically when you define the size
to be 512 X 512, you are basically asking for 512 * 512 = 262144 independent random samples from the normal distribution which are arranged in the form of a 512 X 512 array. It is essentially not different than sampling one random value from the normal distribution 262144 times and reshaping the results yourself.
What you essentially want to do is to create a circularly symmetric 2D array in which the centermost value is equal to or very close to the loc
value you want and values get farther away from loc
the away from center you go. Once you have such a distribution, you can define a normal distribution object using scipy.stats.norm
with the same loc
value and an appropriate scale
. You can then use that normal distribution object to get the probability density (pdf) values for your circularly symmetric 2D array which will mimic the image you want.