2017-04-25 166 views
0

我試圖用Matplotlib顯示一個繪圖,使用一個.txt文件中的數組數據,但是當顯示該數字時,沒有繪圖,並且標籤重複了數字陣列的位置。發生了什麼?matplotlib錯誤。不要顯示陰謀

前奏數據文件是這樣的:

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

如何解決這一問題?

回答

0

變化converted.appendconverted.extend。當您想要傳遞一個系列時,您正將一個嵌套列表傳遞給plt.plot

+0

非常感謝,這解決了我! –