0

I have a function that takes one 2D vector and returns pdf (a scalar) of that point.

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

Now I want to plot this pdf using plot_surface like below: enter image description here This is what I've tried

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]

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.

x, y = np.mgrid[-5:5,-5:5]

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.

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.

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

2
  • 1
    "I tried arange, meshgrid and so on, but could not find a way to make a proper data points" - what was the problem with these methods? Show us your code. Commented Sep 26, 2016 at 18:19
  • @ali_m I just added what I've tried. Thanks for your help! Commented Sep 26, 2016 at 21:00

1 Answer 1

1

Use apply_along_axis

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])
Sign up to request clarification or add additional context in comments.

3 Comments

By the way, why axis is -1?
Doesn't actually matter here, but axis=- makes "x or y" the last axis, which makes (xij, yij) == (xy[i,j,0], xy[i,j,1]) == xy [i,j]. You could a pick a different axis, but then then 1 and 0 would not be last.
Thanks! last axis1

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.