Reputation: 237
I am trying to plot some data from a big file. The data has the following form:
0.025876 139 0
0.030881 140 0
0.030982 141 0
0.035602 142 0
0.035521 143 0
0.038479 144 0
0.040668 145 0
0.040121 146 0
0.037953 147 0
0.039027 148 0
0.038338 149 0
0.047557 139 1
0.045105 140 1
0.044943 141 1
0.042370 142 1
0.042025 143 1
0.038946 144 1
0.037953 145 1
0.033373 146 1
0.030070 147 1
0.029118 148 1
0.025552 149 1
In principle, each line corresponds to a three dimensional point and I would "simply" like to plot a 3d surface generated from these points akin to what I could do with the splot function in gnuplot for those of you that know about it.
Going on the net to find an answer to my problem, I tried the following thing with the matplolib contour function:
#!/usr/bin/python
from numpy import *
import pylab as p
import sys
import mpl_toolkits.mplot3d.axes3d as p3
s = str(sys.argv[1])
f = open(s)
z,y,x = loadtxt(f, unpack = True)
f.close
#x = [1,2,3]
#y = [1,2,3]
#z = [1,8,16]
data = zip(x,y,z)
#map data on the plane
X, Y = meshgrid(arange(0, 89, 1), arange(0, 300, 1))
Z = zeros((len(X),len(Y)),'Float32')
for x_,y_,z_ in data:
Z[x_, y_] = z_ #this should work, but only because x and y are integers
#and arange was done with a step of 1, starting from 0
fig=p.figure()
ax = p3.Axes3D(fig)
ax.contourf(X,Y,Z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p.show()
This piece of code worked actually fine with the vectors x,y and z commented with an hashtag in the above code.
But know that I am trying with the data given above, I get "Inputs x and y must be 1D or 2D" error in matplotlib.
I have read that this could be related to the fact that Z does not have the same shape as X or Y...but I am not sure how to deal with this problem.
By the way, as you probably realized, I am a super newbie in Python and I apologize if the code appears very ugly to some of you.
In any case, any help will be very much welcome.
Thanks !
Fabien
Upvotes: 2
Views: 8008
Reputation: 879571
Using scipy.interpolate.griddata:
import io
import sys
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
import scipy.interpolate as interpolate
content = '''0.025876 139 0
0.030881 140 0
0.030982 141 0
0.035602 142 0
0.035521 143 0
0.038479 144 0
0.040668 145 0
0.040121 146 0
0.037953 147 0
0.039027 148 0
0.038338 149 0
0.047557 139 1
0.045105 140 1
0.044943 141 1
0.042370 142 1
0.042025 143 1
0.038946 144 1
0.037953 145 1
0.033373 146 1
0.030070 147 1
0.029118 148 1
0.025552 149 1'''
data = np.genfromtxt(io.BytesIO(content), dtype=None, names='x, y, z')
# Or, to read from a file:
# data = np.genfromtxt(filename, dtype=None, names='x, y, z')
x, y, z = data['x'], data['y'], data['z']
N = 20
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
X, Y = np.meshgrid(xi, yi)
Z = interpolate.griddata((x, y), z, (X, Y), method='nearest')
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.scatter(data['x'], data['y'], data['z'])
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
# ax.plot_surface(X, Y, Z)
plt.show()
yields
Relevant links:
Upvotes: 1