Hubschr
Hubschr

Reputation: 1335

How to assign a value from an array to an (x,y)-point

I have 3600 x values and 3600 y values. And an array (180x20). I need to plot it as contourplot and assign the first x value and the first y value, to the first array-value (A[0,0]) and so on... How can I do that?

My idea:

x,y = np.meshgrid(x,y)
plt.contourf(x,y,A)

Doesnt work: TypeError: Shape of x does not match that of z: found (3600, 3600) instead of (180, 20). I understand the error, but dont know how to solve it.

Upvotes: 1

Views: 3942

Answers (1)

Joe Kington
Joe Kington

Reputation: 284602

I think you're a bit confused on what meshgrid does. It sounds like you just want reshape.

An explanation of numpy.meshgrid

As an example of what meshgrid does, let's say we have a 5x5 grid of "z" values:

import numpy as np
z = np.random.random((5,5))

And we have the x-coordinates for a row and the y-coordinates for a column:

ny, nx = z.shape
x = np.linspace(5, 7, nx)
y = np.linspace(-2, 8, ny)

print '-----Z-----\n', z, '\n'
print '-----X-----\n', x, '\n'
print '-----Y-----\n', y, '\n'

So, at this point, we have:

-----Z-----
[[ 0.70319561  0.0141277   0.17580355  0.20411183  0.81714624]
 [ 0.45093838  0.18241847  0.27477369  0.4881957   0.62157783]
 [ 0.83172549  0.75278372  0.64856436  0.76651935  0.0152465 ]
 [ 0.50908933  0.51557264  0.9975723   0.39579782  0.71333262]
 [ 0.58998339  0.59205064  0.42716255  0.14138964  0.38212301]]

-----X-----
[ 5.   5.5  6.   6.5  7. ]

-----Y-----
[-2.   0.5  3.   5.5  8. ]

numpy.meshgrid is meant to take those 1D arrays of column and row coordinates and turn them into 2D arrays that match the shape of z:

yy, xx = np.meshgrid(y, x)

print '-----XX----\n', xx, '\n'
print '-----YY----\n', yy, '\n'

This yields:

-----XX----
[[ 5.   5.   5.   5.   5. ]
 [ 5.5  5.5  5.5  5.5  5.5]
 [ 6.   6.   6.   6.   6. ]
 [ 6.5  6.5  6.5  6.5  6.5]
 [ 7.   7.   7.   7.   7. ]]

-----YY----
[[-2.   0.5  3.   5.5  8. ]
 [-2.   0.5  3.   5.5  8. ]
 [-2.   0.5  3.   5.5  8. ]
 [-2.   0.5  3.   5.5  8. ]
 [-2.   0.5  3.   5.5  8. ]]

Working with the data you have

If I'm understanding you correctly, your x and y arrays are each 3600-element lists, and your z-array is 180x20. (Note that 180 * 20 == 3600) Therefore, I think the data you have is equivalent to doing:

yourx, youry = xx.flatten().tolist(), yy.flatten().tolist()

Obviously, your data is a lot larger, but if we extend the example above, it would look like:

---yourx---
[5.0, 5.0, 5.0, 5.0, 5.0, 5.5, 5.5, 5.5, 5.5, 5.5, 6.0, 6.0, 6.0, 6.0, 6.0, 6.5, 6.5, 6.5, 6.5, 6.5, 7.0, 7.0, 7.0, 7.0, 7.0]

---youry---
[-2.0, 0.5, 3.0, 5.5, 8.0, -2.0, 0.5, 3.0, 5.5, 8.0, -2.0, 0.5, 3.0, 5.5, 8.0, -2.0, 0.5, 3.0, 5.5, 8.0, -2.0, 0.5, 3.0, 5.5, 8.0]

Therefore, you just want to reshape your lists into a 2D array of the same shape as z. For example:

print np.reshape(yourx, z.shape)

yields

[[ 5.   5.   5.   5.   5. ]
 [ 5.5  5.5  5.5  5.5  5.5]
 [ 6.   6.   6.   6.   6. ]
 [ 6.5  6.5  6.5  6.5  6.5]
 [ 7.   7.   7.   7.   7. ]]

In other words, you want:

plt.contourf(np.reshape(yourx, z.shape), np.reshape(youry, z.shape), z)

Upvotes: 3

Related Questions