python用数组画图_不同长度的绘图数组

我正在绘制三种不同算法的误差与迭代次数的关系图。它们需要不同的迭代次数来计算,因此数组的长度不同。但我想把这三条线都画在同一个图上。当前,当我使用以下代码时会出现此错误:import matplotlib.pyplot as plt

plt.plot(ks, bgd_costs, 'b--', sgd_costs, 'g-.', mbgd_costs, 'r')

plt.title("Blue-- = BGD, Green-. = SGD, Red=MBGD")

plt.ylabel('Cost')

plt.xlabel('Number of updates (k)')

plt.show()

错误:plt.plot(ks, bgd_costs, 'b--', sgd_costs, 'g-.', mbgd_costs, 'r')

File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/pyplot.py", line 2995, in plot

ret = ax.plot(*args, **kwargs)

File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/axes/_axes.py", line 1331, in plot

for line in self._get_lines(*args, **kwargs):

File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/axes/_base.py", line 312, in _grab_next_args

for seg in self._plot_args(remaining[:isplit], kwargs):

File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/axes/_base.py", line 281, in _plot_args

x, y = self._xy_from_xy(x, y)

File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/axes/_base.py", line 223, in _xy_from_xy

raise ValueError("x and y must have same first dimension")

ValueError: x and y must have same first dimension

更新

多亏了@ibizaman的回答,我才有了这个情节:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值