Python shows two-dimensional PDF function

Is there an elegant way to display a two-dimensional PDF function?

I have a function F (x, y) and I want to illustrate it.

Here is one solution:

Create a meshgrid and calculate the value of each point, then use imshow ()

     1 1.5   2 2.5   3 3.5
    -----------------------
  1| 0   0 0.1 0.2 0.3 0.5
1.5| 0 0.1 0.2 0.3 0.4 0.8
  2| 0 0.1 0.2 0.3 0.5 0.8
2.5| 0 0.2 0.2 0.4 0.4   1
  3| 0 0.1 0.2 0.3 0.5 0.8

However, I do not know whether to use np.vstack or np.meshgrid to generate a "meshgrid".

Can someone tell me how to draw a map above? Or provide a more β€œelegant” way to do this?

Feedback

The solution below works perfectly. This gives a figure as follows: Distribution

+4
source share
1 answer

You can use meshgridto create coordinates:

import numpy as np
x = np.linspace(1, 3.5, 6)
y = np.linspace(1, 3, 5)
X, Y = np.meshgrid(x, y)

F :

z = np.array([F(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = z.reshape(X.shape)

:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('F(X, Y)')
plt.show()

, imshow, F, , 3D-. meshgrid, imshow, .

+3

All Articles