Python implementation reads the txt file and draws a simple code example of a THREE DIMENSIONAL diagram
- 2020-06-15 09:38:50
- OfStack
Children with poor memory should take notes more often!
I just came into contact with python, and recently I needed to draw a 3D diagram. Then I found a lot of data, which made me dizzy. Today, I finally solved it! All right, so much for the code!
# by 3 a 1 D coordinates to draw 3 D scatter
#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 Turn type int type
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()
The most critical step is that string type to int type, before the lack of this 1 step, dead live error, ok, finally done!
I'm going to draw a 3 dimensional line
#
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 Turn type int type
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()
conclusion
That's it for the Python implementation to read the txt file and draw a simple code example of a 3D diagram. Those who are interested can continue to see other related topics on this site. If there is any deficiency, please let me know. Thank you for your support!