0

OK... please be patient with me, as I am not really sure how to ask this question.

I am trying to generate a 2-D contour plot of some data (generated by a calculation at points on a Blender plane). The order in which I get these data points is random, but I do know the x,y coordinates for each z value. In other words I have an unsorted collection of [x,y,z] triplets.

My question is... what is the simplest way for me to mash these data points into a set of arrays that I can contour plot with Matplotlib?

4

1 回答 1

1

这假设a)您的数据位于均匀间隔的网格上,并且b)您拥有所有网格点

from pylab import * # mostly to make my fake data work
import copy

# make some fake data
X, Y = np.meshgrid(range(10), range(10))
xyz = zip(X.flat, Y.flat, np.random.rand(100)) # make sure you have a list of tuples
xyz_org = copy.copy(xyz)

# randomize the tuples
shuffle(xyz)
# check we changed the order
assert  xyz != xyz_org
# re-sort them
xyz.sort(key=lambda x: x[-2::-1]) # sort only on the first two entries 

# check we did it right
assert xyz == xyz_org

# extract the points and re-shape to a grid
X_n, Y_n, z = [np.array(_).reshape(10, 10) for _ in zip(*xyz)]

# check we re-created X and Y correctly
assert np.all(X_n == X)
assert np.all(Y_n == Y)

# make the plot
plt.contour(X_n, Y_n, z)
于 2013-08-18T18:28:24.933 回答