1

I'm new to python and trying to plot a gaussian distribution having the function defined as

enter image description here

I plotted normal distribution P(x,y) and it's giving correct output. code and output are below.

Code : enter image description here

Output : enter image description here

Now I need to plot a conditional distribution enter image description here and the output should like enter image description here. to do this I need to define a boundary condition for the equation. I tried to define a boundary condition but it's not working. the code which I tried is enter image description here but it's giving wrong output enter image description here please help me how to plot the same.

Thanks,

1
  • 3
    It would be better to put the python codes as a text code instead of picture. Commented Nov 3, 2016 at 12:27

2 Answers 2

2

You used the boundary condition on the wrong parameter, try to do it after creating the grid points.

R = np.arange(-4, 4, 0.1)
X, Y = np.meshgrid(R, R)

then validate X and Y based on the condition

valid_xy = np.sqrt(X**2+Y**2) >= 1

X = X[valid_xy]
Y = Y[valid_xy]

Then continue with the rest of the code.

Update

If you want just to reset values around the peak to zero, you can use the following code:

import numpy as np
import matplotlib.pyplot as plt

R = np.arange(-4, 4, 0.1)
X, Y = np.meshgrid(R, R)

Z = np.sum(np.exp(-0.5*(X**2+Y**2)))
P = (1/Z)*np.exp(-0.5*(X**2+Y**2))

# reset the peak
invalid_xy = (X**2+Y**2)<1
P[invalid_xy] = 0

# plot the result 

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X, Y, P, s=0.5, alpha=0.5)
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

but with this I'm facing one issue. the inner circle is not coming.
Thanks for the help :) I have one more doubt can you answer the same ?? I'm struggling to understand "Marginalise the conditional distribution(above solved) over y and plot Q(x)" and please let me know where I can learn more about probability plotting via python.
projection = '3d' does not work, until from mpl_toolkits.mplot3d import axes3d is used.
0

You can't use np.meshgrid anymore because it will output a matrix where the coordinates of X and Y form a grid (hence its name) and not a custom shape (a grid minus a disc like you want):

However you can create your custom grid the following way:

R = np.arange(-,4,0.1)
xy_coord = np.array(((x,y) for x in R for y in R if (x*x + y*y) > 1))
X,Y = xy_coord.transpose()
X
# array([ 0. ,  0. ,  0. , ...,  3.9,  3.9,  3.9])
Y
# array([ 1.1,  1.2,  1.3, ...,  3.7,  3.8,  3.9])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.