Plotting function of (x,y) with 3D plot
<p>I have a function that takes one 2D vector and returns pdf (a scalar) of that point.</p>

<p>As an illustration, <code>myPDF( np.array[4,4] )</code> returns a single pdf value at &lt;4,4>. This function cannot take a list of vectors, and it only accepts one  2D vector as an argument. </p>

<p>Now I want to plot this pdf using plot_surface like below:
<a href="http://i.stack.imgur.com/tmbHI.png" rel="nofollow"><img src="http://i.stack.imgur.com/tmbHI.png" alt="enter image description here"></a>
This is what I've tried</p>

<pre><code>x = np.arange(-10,10)
y = np.arange(-10,10)
z = [ myPDF( np.array([xx,yy])) for xx in x for yy in y]
xy = [ [xx, yy] for xx in x for yy in y]
</code></pre>

<p>And here I am stuck. I have to somehow reshape z and xy in some proper forms to use them with plot_surface, but I do not now how to do so. I also tried meshgrid, but did not know how to convert different shapes of data into legitimate forms for plot_surface. </p>

<pre><code>x, y = np.mgrid[-5:5,-5:5]
</code></pre>

<p>If I use mgrid as above, it gives me 10x10 matrix. Then, I have to generate corresponding z values in 10x10 matrix using my function, but could not find a way to decompose x and y, generate z values and reformat z values in a proper form. </p>

<p>The major problem here is that I have little knowledge in manipulating data, so could not find a good way of doing this task. Thus, I'd like to learn a proper way of generating a dataset for plot_surface using my pdf function. </p>

<p>What's a good way to generate data points to plot like this using my pdf function? Thanks for helping this newB!</p>

<p>Use <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html" rel="nofollow"><code>apply_along_axis</code></a></p>

<pre><code>xy = np.stack(np.mgrid[-5:5,-5:5], axis=-1)      # xy[0,0] = x, y
z = np.apply_along_axis(myPDF, axis=-1, arr=xy)  # z[0,0] = myPDF(xy[0,0])
</code></pre>

python numpy plot scipy