0

Suppose I have a coordinate grid with a few points(masses) sprinkled in the grid. I can create this using:

import numpy as np
import matplotlib.pyplot as plt 

points = np.array([[0,1],[2,1],[3,5]]) # array containing the coordinates of masses(points) in the form [x,y]

x1, y1 = zip(*points)

Now, I can plot using :

plt.plot(x1,y1,'.')

Plot of points

Now, say I create a 2D meshgrid using:

x = np.linspace(-10,10,10)
y = np.linspace(-10,10,10)
X,Y = np.meshgrid(x,y)

Now, what I want to do is to create a 2D array 'Z',(a map of the masses)that contains masses at the locations that are in the array points. When I mean masses, I just mean a scalar at those points. So I could do something like plt.contourf(X,Y,Z). The problem I'm having is that the indices for Z cannot be the same as the coordinates in points. There has to be some sort of conversion which I'm not able to figure out. Another way to look at it is I want:

Z[X,Y] = 1

I want Z to have 1's at locations which are specified by the array points. So the essence of the problem is how do I calculate the X and Y indices such that they correspond to x1, y1 in real coordinates.

For example, if I simply do Z[x1(i),y1(i)] = 1, contourf gives this: Instead I want the spikes to be at (0,1),(2,1),(3,5). enter image description here

7
  • Why not do simply Z[x[i], y[i]] = 1 ? Commented Nov 1, 2017 at 15:30
  • That just makes me have the masses at the pixel values of Z, so basically its own indices. The coordinates of the masses are not pixel values but actual numbers on Cartesian grid. Commented Nov 1, 2017 at 15:31
  • I'm not sure I get what you're trying to do exactly. Commented Nov 1, 2017 at 15:35
  • I want Z[x1,y1] = 1 but if I actually type that in code, Python assigns 1 to the index values of Z, not to x1 and y1 if I use X and Y as the meshgrids... Commented Nov 1, 2017 at 15:40
  • If I understand you correctly, you want a constant function (equal to 1) evaluated on a grid represented by x and y coordinates ? Then just do Z = np.ones(X.shape)). You can then use it in plt.contourf(x, y, Z). Commented Nov 1, 2017 at 15:44

1 Answer 1

1

To have 1 at the coordinates specified by x1, y1 and zeros everywhere else, I would write it like this:

x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
Z = np.zeros((len(y), len(x)))
for i in range(len(x1)):
    Z[10 + y1[i], 10 + x1[i]] = 1

Then you should be able to write plt.contourf(x, y, Z).

Tell me if that gives you the desired result.

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

9 Comments

I've tried this method, problem is that it only works if the pixel values are the same as the integer values. As in, 1 pixel corresponds to 1 unit distance in x, so this wouldn't work say if I had x = np.linspace(-50,50,21)
Then you should make sure that you have integer values in your grid. Or at least that the values inside x1 and y1 are actually points on your grid.
so the main issue is, say I have a number 0.5 inside x1, then I can't use this method, is there a way to do it for non integer numbers? If I do have a grid containing say [0,0.5,1], I still can't assign the coordinate to the 2nd pixel in an efficient manner...
You could try having a fine enough resolution on your grid. For example, if you know your points in x1 and y1 are defined with a precision of 0.01, then create your grid with that precision as well. Only problem is that you'll start getting a really big grid for small precision.
I think you're getting confused by the linear transformation. The formula is Z[10 + x1[i],...] because the grid starts at -10 with precision 1. If your grid starts at -10 with precision 0.001, then it would be Z[10 / 0.001 + x1[i] / 0.001,...] = Z[10000 + 1000 * x1[i],...]. And you would declare your grid with linspace(-10, 10, 20001)
|

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.