Serhiy Serhiy - 1 month ago 14
Python Question

convert "meshvectors" to meshgrid in Python

I have three 1D vectors, first two are coordinates and third is a value. I can't figure out a simple way to plot them using

pcolor
or
imshow
because these functions accept the meshgrid values, and the funny thing is that my vectors are already a vector representation of meshgrid:

csv_data=StringIO("""subsample, colsample, val
0.6, 0.6, 0.87277
0.7, 0.6, 0.87408
0.8, 0.6, 0.87294
0.9, 0.6, 0.87242
0.6, 0.7, 0.87341
0.7, 0.7, 0.87238
0.8, 0.7, 0.87290
0.9, 0.7, 0.87285
0.6, 0.8, 0.87268
0.7, 0.8, 0.87392
0.8, 0.8, 0.87353
0.9, 0.8, 0.87223
0.6, 0.9, 0.87147
0.7, 0.9, 0.87271
0.8, 0.9, 0.87247
0.9, 0.9, 0.87324
""")

df = pd.read_csv(csv_data, sep=', ')

X, Y, Z = df.subsample.values, df.colsample.values, df.val.values


As you can see for each value of X there is a full set of Y values. And for each value of Y there is a complete set of X values with Z already calculated. How can I construct x, y and z as meshgrids based on this data?

Answer

The data must be in 2D array's, this can be easily done with your data with the reshape method.

X = X.reshape((4,4))
Y = Y.reshape((4,4))
Z = Z.reshape((4,4))

plt.contourf(X,Y,Z)