【问题标题】:Error with matplotlib. Don't display plotmatplotlib 出错。不显示情节
【发布时间】:2023-03-03 04:04:01
【问题描述】:

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

然后给我看这个情节:

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

如何解决这个问题?

【问题讨论】:

    标签: python arrays matplotlib plot


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2019-08-31
      • 2014-09-13
      • 2016-03-24
      • 2020-08-20
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多