简体   繁体   中英

plot a 3d surface plot using matplotlib

Hello all I have lists of x coordinates and y and Z. Now I want to use matplotlib to plot a 3d surface. I tried to write the code, but the output was blank. Can any one help me with this: this is the code:

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
z=[-3,-6,10,8,23,75]
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.plot_surface(x,y, z)
#ax.view_init(90, -90)
plt.show()

The values I gave are random but generally I get the values in a list like the above x,y,z lists. Now can anyone point out the error I making in the code so that i can correct it I want a graph like the one below:

在此输入图像描述

The code which I have written for plotting 3d surface is below:

from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import math
f=open(filedir+'/'+'temp_1.txt','r')
for line in f:
    line=line.strip()
    temp=line.split(',')
    temp1=float(temp[0])
    temp2=float(temp[1])
    temp3=float(temp[2])
    #num_z=math.log(temp3)
    num_x=temp1
    num_y=temp2
    num_z=temp3
    x.append(num_x)
    y.append(num_y)
    z.append(num_z)
for num in range(len(z)):
      if z[num] != 0:
        z1.append(math.log(z[num]))
      else:
        z1.append(0)
xi=np.linspace(min(x),max(x))
yi=np.linspace(min(y),max(y))

min_x=min(x)-2
max_x=max(x)+2
min_y=min(y)-2
max_y=max(y)+20
min_z=min(z1)-2
max_z=max(z1)+2
pi=3.14
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.set_xlabel('Frequency')
ax.set_ylabel('Distance')
ax.set_zlabel('Sound_Pressure Level')
X,Y=np.meshgrid(xi,yi)
Z=griddata(x,y,z1,xi,yi)
surf=ax.plot_surface(X,Y,Z,rstride=5,cstride=5,cmap=cm.jet,linewidth=1,antialiased=True)
#ax.view_init(90, -90)
cset = ax.contour(X, Y, Z, zdir='z', offset=min_z, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=min_x, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=max_y, cmap=cm.coolwarm)

ax.set_xlim3d(min_x, max_x)
ax.set_ylim3d(min_y,max_y)
ax.set_zlim3d(min_z,max_z)
plt.show()

The input looks like this and it's in a .txt file:

1.0000000E+01,3,4.826432E-11
1.0000000E+01,4,4.342127E-11
1.0000000E+01,5,3.861855E-11
1.0000000E+01,6,3.588365E-11
1.0000000E+01,7,3.244975E-11
1.0000000E+01,8,2.986569E-11
1.0000000E+01,9,2.740276E-11
1.0000000E+01,10,2.604628E-11
2.0000000E+01,-11,4.177395E-11
2.0000000E+01,-10,4.658516E-11
2.0000000E+01,-9,5.209122E-11
2.0000000E+01,-8,5.575429E-11
2.0000000E+01,-7,5.808602E-11
2.0000000E+01,-6,5.876480E-11
2.0000000E+01,-5,5.803726E-11
2.0000000E+01,-4,5.566828E-11
2.0000000E+01,-3,5.084253E-11
2.0000000E+01,-2,4.771793E-11
2.0000000E+01,-1,4.176435E-11
2.0000000E+01,0,3.828995E-11

This is a sample input file and it has 1700 lines like this. the output is: 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM