Rudresha Parameshappa
Rudresha Parameshappa

Reputation: 3926

plotting conditional distribution in python

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,

Upvotes: 1

Views: 6128

Answers (2)

hashmuke
hashmuke

Reputation: 3325

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()

Upvotes: 2

JMat
JMat

Reputation: 737

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])

Upvotes: 0

Related Questions