【问题标题】:ValueError: x and y must have same first dimension , ipython 3.5ValueError:x 和 y 必须具有相同的第一维,ipython 3.5
【发布时间】:2016-07-23 12:40:04
【问题描述】:

我是 ipython 的新手。我的 ipython 中有这段 sn-p 代码

def derivative(x):
    return 3*(x^2)
derivative_estimate = lambda x: difference_quotient(cube, x, h=0.00001)

x = range(-10, 10)
import matplotlib.pyplot as plt
plt.title("Actual Derivatives vs Estimates")
x = range(-10,10)
plt.plot(x, map(derivative, x), 'rx', label = 'Actual')           # red  x
plt.plot(x, map(derivative_estimate, x), 'b+', label = 'Estimate')  # blue +
plt.show()

当我尝试运行它时,它会显示这些错误


ValueError                                Traceback (most recent call last)
<ipython-input-12-211954bb4dc5> in <module>()
      6 plt.title("Actual Derivatives vs Estimates")
      7 x = range(-10,10)
----> 8 plt.plot(x, map(derivative, x), 'rx')           # red  x
      9 plt.plot(x, map(derivative_estimate, x), 'b+')  # blue +
     10 plt.show()

C:\Users\User\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
   3152         ax.hold(hold)
   3153     try:
-> 3154         ret = ax.plot(*args, **kwargs)
   3155     finally:
   3156         ax.hold(washold)

C:\Users\User\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1809                     warnings.warn(msg % (label_namer, func.__name__),
   1810                                   RuntimeWarning, stacklevel=2)
-> 1811             return func(ax, *args, **kwargs)
   1812         pre_doc = inner.__doc__
   1813         if pre_doc is None:

C:\Users\User\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
   1422             kwargs['color'] = c
   1423 
-> 1424         for line in self._get_lines(*args, **kwargs):
   1425             self.add_line(line)
   1426             lines.append(line)

C:\Users\User\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
    384                 return
    385             if len(remaining) <= 3:
--> 386                 for seg in self._plot_args(remaining, kwargs):
    387                     yield seg
    388                 return

C:\Users\User\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    362             x, y = index_of(tup[-1])
    363 
--> 364         x, y = self._xy_from_xy(x, y)
    365 
    366         if self.command == 'plot':

C:\Users\User\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
    221         y = _check_1d(y)
    222         if x.shape[0] != y.shape[0]:
--> 223             raise ValueError("x and y must have same first dimension")
    224         if x.ndim > 2 or y.ndim > 2:
    225             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension,

谁能帮我解决这个问题?

【问题讨论】:

  • 仅供参考:在 Python 中 ^ 不是求幂;它是“按位异或”。使用x**2 表示“x 平方”。
  • 是的,我后来想通了

标签: python-3.x matplotlib ipython


【解决方案1】:

Matplotlib 的 plot 函数无法处理 map 返回的迭代器。在调用plot之前将迭代器展开为一个列表:

plt.plot(x, list(map(derivative, x)), 'rx', label = 'Actual')

【讨论】:

  • 是的,这就是答案。好吧,我太忙了,无法回答我自己的帖子。但你已经这样做了。所以接受它:)
【解决方案2】:

尝试导入numpy,然后使用numpy.linspacenumpy.arange 而不是range。我相信您的问题是在您的函数derivative 中产生的,您在其中使用3*x。如果x是一个列表(从标准range返回,3*x将返回[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]如果x=[1, 2, 3, 4]。你想要得到的是3*x = [3, 6, 9, 12],如果你使用就会出现这种情况numpy.linspacenumpy.arange 代替。

文档链接:numpy.arangenumpy.linspace

【讨论】:

  • 不,和numpy没有任何关系
  • x-list 的长度是多少,从函数导数返回的列表的长度是多少?如果我使用x = range(-10, 10),我会得到len(x) = 20len(derivative(x)) = 60
猜你喜欢
  • 2016-11-10
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2015-06-01
  • 1970-01-01
相关资源
最近更新 更多