Otávio Augusto
Otávio Augusto

Reputation: 45

Error with matplotlib. Don't display plot

i'm trying to display a plot with Matplotlib, using a array data from a .txt file, but when the figure is showed, don't have a plot, and the label is repeated with the number of positions of the array. What's happening?

The intro datafile is like this:

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

Then show me this plot:

Plot from array

And that's the code:

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

How to fix this?

Upvotes: 0

Views: 196

Answers (1)

Paul Brodersen
Paul Brodersen

Reputation: 13031

Change converted.append to converted.extend. You are passing a nested list to plt.plot, when you want to pass a single series.

Upvotes: 1

Related Questions