【发布时间】:2018-05-21 01:29:45
【问题描述】:
我正在尝试在 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 新手,我确信我错过了一些基本的东西。我已经尝试在谷歌中搜索答案,但我没有得到任何确定的答案。
谢谢!
【问题讨论】:
-
使用这个:
fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(1, 1, 1) x = [] y = [] for i in range(50000): if (i % 500 == 0): y.append(np.random.random()) x.append(i) ax.plot(x, y, '-', label ='Cost') plt.show()
标签: python python-2.7 matplotlib plot