matplotlib出错。不要显示情节

时间:2017-04-25 12:55:21

标签: python arrays matplotlib plot

我正在尝试使用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

然后告诉我这个情节:

Plot from array

这就是代码:

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')

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

converted.append更改为converted.extend。当您想传递单个系列时,您正在将嵌套列表传递给plt.plot