从CSV文件创建3D散点图

时间:2017-04-19 01:27:57

标签: python csv numpy matplotlib 3d

我想从csv文件创建一个三维散点图,但我无法打印三维散点图。我的代码就像这样

import matplotlib.pyplot as plt
import numpy as np 

fig = plt.figure()
ax1 = fig.add_subplot(111,projection='3d')

x, y, z = np.loadtxt('3d_sample.txt', delimiter=',', unpack=True)

ax1.scatter(x,y,z)
plt.show()

1 个答案:

答案 0 :(得分:4)

您需要添加:

from mpl_toolkits.mplot3d import Axes3D

例如,

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax1 = fig.add_subplot(111,projection='3d')

x= np.array([1,2,3,4])
y=np.array([5,6,7,8])
z=np.array([9,10,11,12])

ax1.scatter(x,y,z)
plt.show()

给出

enter image description here