我正在尝试使用Matplotlib显示一个绘图,使用来自.txt文件的数组数据,但是当显示图形时,没有绘图,并且标签重复使用的位置数量阵列。发生了什么事?
介绍数据文件是这样的:
0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,2.0, 2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,25.0,25.0,25.0,25.0,25.0,25.0,25.0,25.0,25.0,25.0
然后告诉我这个情节:
这就是代码:
import matplotlib.pyplot as plt
import codecs
converted = []
reward = open('reward_5_clusters','r')
acum = reward.readlines()
for line in acum:
if line.startswith(codecs.BOM_UTF8):
line = line[len(codecs.BOM_UTF8):]
x = line.split(', ')
converted.append(x)
plt.plot(converted, label='5 clusters')
plt.ylabel('Reward')
plt.xlabel('Time')
plt.title('Cumulative Reward')
plt.grid(True)
plt.legend(loc=0)
plt.show(block=False)
plt.savefig('cumulative_reward.png')
如何解决这个问题?
答案 0 :(得分:0)
将converted.append
更改为converted.extend
。当您想传递单个系列时,您正在将嵌套列表传递给plt.plot
。