简体   繁体   English

ValueError:x和y必须具有相同的第一维,即ipython 3.5

[英]ValueError: x and y must have same first dimension , ipython 3.5

I am new to ipython. 我是ipython的新手。 I have this snippet of code in my ipython 我的ipython中有此代码段

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()

When I try to run this, it shows me these errors 当我尝试运行此命令时,它向我显示了这些错误


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,

Can anyone please help me to solve this issue? 谁能帮我解决这个问题?

Matplotlib's plot function can't handle the iterator returned by map . Matplotlib的plot函数无法处理map返回的迭代器。 Expand the iterator to a list before calling plot : 在调用plot之前,将迭代器扩展到一个列表:

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

Try to import numpy and then use either numpy.linspace or numpy.arange instead of range . 尝试导入numpy ,然后使用numpy.linspacenumpy.arange代替range I believe your problem is created inside your function derivative , where you take 3*x . 我相信您的问题是在函数derivative内部产生的,您取3*x If x is a list (which is returned from the standard range , 3*x will return [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] if x=[1, 2, 3, 4] . What you want to get is 3*x = [3, 6, 9, 12] , and this will be the case if you use numpy.linspace or numpy.arange instead. 如果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.linspace 3*x = [3, 6, 9, 12] ,如果您使用numpy.linspacenumpy.arange会发生这种情况。

Documentation links: numpy.arange and numpy.linspace 文档链接: numpy.arangenumpy.linspace

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: x 和 y 必须具有相同的第一维,但具有形状 (2140699,) 和 (4281398,) - ValueError: x and y must have same first dimension, but have shapes (2140699,) and (4281398,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (165,) 和 (166,) - ValueError: x and y must have same first dimension, but have shapes (165,) and (166,) ValueError: x 和 y 必须具有相同的第一个维度,但具有形状 (99955,) 和 (15000,) - ValueError: x and y must have same first dimension, but have shapes (99955,) and (15000,) 如果 ValueError: x 和 y 必须具有相同的第一维,但形状为 (32,) 和 (31, 5),我该怎么办? - What do I do if ValueError: x and y must have same first dimension, but have shapes (32,) and (31, 5)? Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' numpy数组具有相同的形状,但出现ValueError:x和y必须具有相同的第一维 - Numpy arrays same shape but getting ValueError: x and y must have same first dimension 数据科学python错误-ValueError:x和y必须具有相同的第一维 - Data science python error- ValueError: x and y must have same first dimension 绘制序列会导致:“ValueError:x和y必须具有相同的第一维” - plotting a sequence results in: “ValueError: x and y must have same first dimension” 错误消息:x和y必须具有相同的第一尺寸,但形状为(23,)和(1,) - Error message : x and y must have same first dimension, but have shapes (23,) and (1,) Matplotlib 错误:x 和 y 没有相同的第一维 - Matplotlib error: x and y do not have the same first dimension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM