1

I'm just a beginner Python user so bear with me,

I've created a 2d XY grid using np.meshgrid, and would like to know how to assign values to some of the points in the grid. The application is to set a mass to certain points in the grid to model point masses. My code as of now is as follows:

import numpy as np
x = np.arange(11)
y = np.arange(11)

X, Y = np.meshgrid(x,y)

Thanks a lot!

4
  • what do you mean by "assign values to some of the points in the grid." like have a Z array of values for each X,Y coordinate? Commented Jun 4, 2016 at 16:30
  • well yes something like that, but I couldn't think of how to relate the Z as a function of X and Y coordinates and then say, plot it as a contour to visualise it Commented Jun 4, 2016 at 16:44
  • @karasinski gave you an answer for what you seem to be asking. You can give any value directly by attribution, ´Z[5,5] = 5´. What is your grid supposed to represent? Are you working in estimation? Commented Jun 4, 2016 at 20:18
  • Could you show me an example code for the entire process? i really am missing out on something basic here my bad. The grid merely represents coordinates and I want to include point masses at some points. I have to use it to model a gravitational lensing system. Commented Jun 4, 2016 at 20:23

1 Answer 1

1

Since you're a beginner Python user, I highly recommend you take the time to check out the documentation for the packages you are using. Does this example from matplotlib's documentation answer your question?

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

Alternatively, you could also view the numpy documentation for the meshgrid function, which gives a similar example:

x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
xx, yy = meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,z)

EDIT

If you want to manually set the values of Z, you can do something along the lines of

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(11)
y = np.arange(11)
X, Y = np.meshgrid(x,y)
Z = np.zeros_like(X)
Z[5][5] = 5
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion! but I did come across these examples before, and I'm comfortable with defining a function z over the entire x y coordinate plane. What I seem to be stuck at is say, letting (x,y) = (5,5) have a z value of 5. A particular value for a particular coordinate. I'm not sure how to go about doing that? An example would be, say I have a grid of 10 x 10. I want the (5,5) coordinate to have a magnitude of 5. How would I define this?

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.