【发布时间】:2021-08-13 21:58:39
【问题描述】:
我有一个格式如下的文本文件。
1 10 3
1 9 2
1 4 5
2 10 2
2 6 5
3 4 3
3 5 4
3 8 1
第一列代表玩家。有3名教练。第二列代表玩家。总共有10名球员。第三列代表不同教练给每个球员的评分(最小值可以是1,最大值可以是5)。请注意,并非所有玩家都被评分,只有部分玩家被评分。对于我拥有的数据,我基本上想将其绘制为 python 中的 3d 函数。
我想知道最好的方法是什么?
我的方法
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
with open("test.txt") as f:
data = f.read()
data = data.split('\n')
x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[1] for row in data]
z = [row.split(' ')[2] for row in data]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
我的错误
File "solution.py", line 10, in <module>
y = [row.split(' ')[1] for row in data]
IndexError: list index out of range
【问题讨论】:
-
猜测一下,您的数据文件中有一个空行。
标签: python