Python 打印连续曲线-MatplotLib

Python 打印连续曲线-MatplotLib,python,python-2.7,matplotlib,plot,Python,Python 2.7,Matplotlib,Plot,我正在尝试用Python实现一个神经网络,我想画出每次迭代的成本 下面是我当前代码的样子- import matplotlib.pyplot as plt fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(1, 1, 1) for i in range(50000): if (i % 500 == 0): y = np.random.random() # Cost Function. ax

我正在尝试用Python实现一个神经网络,我想画出每次迭代的成本

下面是我当前代码的样子-

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)

for i in range(50000):

    if (i % 500 == 0):
        y = np.random.random()  # Cost Function.
        ax.scatter(i, y, label ='Cost')

plt.show()

这是输出-

问题 该图不显示一条连续曲线。相反,它以不同的颜色显示不同的点,在每次迭代中描绘
(i,y)
元组

此外,“标签”在图例上打印了100次,这显然不是我想要的

我试图打印一条连续曲线和一个图例

我尝试了
ax.plot()
而不是
ax.scatter()
,但它不起作用

有人能帮忙吗?我是Python新手,我确信我遗漏了一些基本的东西。我试着在谷歌上搜索答案,但我没有得到任何明确的答案


谢谢

使用
散点
将始终绘制点。
plot
功能具有大量参数选择(包括打印点的能力),默认情况下,打印点对之间的线段。在
循环的
中的每个步骤中,为
绘图
单个点
x,y
,例如:

for x,y in zip(range(10), range(10)):
    plt.plot(x, y)
不会显示任何内容。这是因为matplotlib试图在
x
y
的每个输入值之间画一条线。由于每一步只传递一个值,因此它从
(x,y)
(None,None)
绘制了一条线,导致没有任何线

要绘制一条连续线,您需要将所有坐标对收集到一个iterable(列表、数组等)中,并将它们传递到
plot

x = []
y = []
for i in range(50000):
    if (i % 500 == 0):
        x.append(i)
        y.append(np.random.random())  # Cost Function.
ax.plot(x, y, label ='Cost')

使用这个:
fig=plt.figure(figsize=(10,8))ax=fig.add_子图(1,1,1)x=[]y=[]用于范围(50000)内的i:if(i%500==0):y.append(np.random.random())x.append(i)ax.plot(x,y,'-',label='Cost')plt.show()