python读取txt文件画三维图

引用Mirror_Yu_Chen,详细请查看原文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#由三个一维坐标画三维散点
#coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
x = []
y = []
z = []
f = open("data\record.txt")
line = f.readline()
while line:
c,d,e = line.split()
x.append(c)
y.append(d)
z.append(e)
line = f.readline()
f.close()
#string型转int型
x = [ int( x ) for x in x if x ]
y = [ int( y ) for y in y if y ]
z = [ int( z ) for z in z if z ]
print x
fig=plt.figure()
ax=Axes3D(fig)
ax.scatter3D(x, y, z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()

最关键的步骤就是那个string类型转int类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#画三维线
#coding:utf-8
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
x = []
y = []
z = []
f = open("data\record.txt")
line = f.readline()
while line:
c,d,e = line.split()
x.append(c)
y.append(d)
z.append(e)
line = f.readline()
f.close()
#string型转int型
x = [ int( x ) for x in x if x ]
y = [ int( y ) for y in y if y ]
z = [ int( z ) for z in z if z ]
#print x
fig=plt.figure()
ax = fig.gca(projection = '3d')
ax.plot(x,y,z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()