Matplotlib只能绘制散点图

时间:2017-09-29 17:17:42

标签: python python-2.7 matplotlib scatter linegraph

[Python 2.7.12]

[Matplotlib 1.5.1]

每个扫描周期我的代码都会产生“最高”分数。我想绘制一段时间内的表现。我已将代码简化为以下示例:

import matplotlib.pyplot as plt
from matplotlib import lines
import random
count = 1

plt.axis([0, 1000, 0, 100])
plt.ion()

while True:
    count += 1
    a=random.randint(1, 50)
    plt.plot(count, a,'xb-')
    plt.pause(0.05)

plt.show()

我的目标是制作折线图。问题是我设置的线条样式,它没有生效。它只绘制散点图类型。然而,我能够改变它是一个点还是一个'X'标记。

或者问题是分数是“情节还是忘记”,所以它没有什么可以吸引的?

编辑:绘图将实时完成

1 个答案:

答案 0 :(得分:2)

你需要至少2分才能画一条线。您可以在每个步骤中存储和使用以前的状态。

import matplotlib.pyplot as plt
from matplotlib import lines
import random

x = 1

plt.axis([0, 1000, 0, 100])
plt.ion()

y_t1 = random.randint(1, 50)
plt.plot(1, y_t1, 'xb')
plt.pause(0.05)

while True:
    x += 1
    y_t2 = random.randint(1, 50)
    plt.plot([x - 1, x], [y_t1, y_t2], 'xb-')
    y_t1 = y_t2
    plt.pause(0.05)

plt.show()

enter image description here