Matplotlib 只能绘制散点图

Matplotlib can only plot scatter graph

[Python 2.7.12]

[Matplotlib 1.5.1]

我的代码的每个扫描周期都会产生一个 'top' 分数。我想随着时间的推移绘制性能。我已将代码简化为以下示例:

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' 标记。

或者问题在于分数是 'plot and forget' 所以它没有什么可借鉴的?

编辑:绘图将实时完成

您至少需要 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()