ValueError:使用 Sage 绘图时“解包的值太多”

Posted

技术标签:

【中文标题】ValueError:使用 Sage 绘图时“解包的值太多”【英文标题】:ValueError: "too many values to unpack" when plotting with Sage 【发布时间】:2012-07-01 10:27:46 【问题描述】:

到目前为止,这是我的代码。 我有 Python 2.7(我今天和 Sage 一起下载了它)并正在从终端在 Mac OS X 10.6.8 上运行它(不过,我确实计划在弄清楚发生了什么后立即从 notebook() GUI 运行它继续出现这些错误)。

sage: import scipy
sage: import numpy
sage: import matplotlib
sage: cd /Users/nickscomputer/Desktop
/Users/nickscomputer/Desktop
sage: DATA = numpy.loadtxt("PracticeDat.txt")
sage: DATA
array([[   1.,    1.],
   [   2.,    4.],
   [   3.,    9.],
   [   4.,   16.],
   [   5.,   25.],
   [   6.,   36.],
   [   7.,   49.],
   [   8.,   64.],
   [   9.,   81.],
   [  10.,  100.]])
sage: X = DATA[:,0]
sage: Y = DATA[:,1]
sage: plot(X,Y)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/nickscomputer/Desktop/<ipython console> in <module>()

/Applications/sage/local/lib/python2.7/site-packages/sage/misc/decorators.pyc in wrapper(*args, **kwds)
    685                     kwds[new_name] = kwds[old_name]
    686                     del kwds[old_name]
--> 687             return func(*args, **kwds)
    688         
    689         return wrapper

 /Applications/sage/local/lib/python2.7/site-packages/sage/misc/decorators.pyc in wrapper(*args, **kwds)
    532                 options['__original_opts'] = kwds
    533             options.update(kwds)
--> 534             return func(*args, **options)
    535 
    536         #Add the options specified by @options to the signature of the wrapped

/Applications/sage/local/lib/python2.7/site-packages/sage/plot/plot.pyc in plot(funcs, *args, **kwds)
   1024         # if there is one extra arg, then it had better be a tuple
   1025         elif n == 1:
-> 1026             G = _plot(funcs, *args, **kwds)
   1027         elif n == 2:
   1028         # if there are two extra args, then pull them out and pass them as a tuple

/Applications/sage/local/lib/python2.7/site-packages/sage/plot/plot.pyc in _plot(funcs, xrange,     parametric, polar, fill, label, randomize, **options)
   1095         return Graphics()
   1096     funcs, ranges = setup_for_eval_on_grid(funcs, [xrange], options['plot_points'])
-> 1097     xmin, xmax, delta = ranges[0]
   1098     xrange=ranges[0][:2]
   1099     #parametric_plot will be a list or tuple of two functions (f,g)

ValueError: too many values to unpack

我试过做一个 list_plot,但我得到了这个错误:

sage: list_plot(X,Y)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/nickscomputer/Desktop/<ipython console> in <module>()

/Applications/sage/local/lib/python2.7/site-packages/sage/misc/decorators.pyc in wrapper(*args, **kwds)
    532                 options['__original_opts'] = kwds
    533             options.update(kwds)
--> 534             return func(*args, **options)
    535 
    536         #Add the options specified by @options to the signature of the wrapped

/Applications/sage/local/lib/python2.7/site-packages/sage/plot/plot.pyc in list_plot(data, plotjoined, **kwargs)
   1561     """
   1562     from sage.plot.all import line, point
-> 1563     if data ==  or data == () or data == []:
   1564         return Graphics()
   1565     if isinstance(data, dict):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我也试过这个选项,它会生成空的绘图轴和这个错误:

sage: plot((X,Y))
verbose 0 (1978: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate  function at 200 points.
verbose 0 (1978: plot.py, generate_plot_points) Last error message: ''numpy.ndarray' object is not callable'
verbose 0 (1978: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 200 points.
verbose 0 (1978: plot.py, generate_plot_points) Last error message: ''numpy.ndarray' object is not callable'

【问题讨论】:

请注意,Sage 包含 Python 2.7:您只需运行 sage --python 【参考方案1】:

使用list_plot。它的文档说

如果您有单独的 x 值和 y 值列表,您想要 相互绘制,使用“zip”命令制作一个列表 其条目是 (x,y) 值对,并将结果输入 “列表图”

所以特别要使用list_plot(zip(X,Y))。或者,将DATA 转换为单个列表:使用list_plot(DATA.tolist())

【讨论】:

【参考方案2】:

我还要指出,我喜欢将line()linestyle='' 选项一起使用,这样我可以获得更多的matplotlib 选项(出于某种原因,list_plotpoints 不能很好地做到这一点):

DATA = [[   1.,    1.],
   [   2.,    4.],
   [   3.,    9.],
   [   4.,   16.],
   [   5.,   25.],
   [   6.,   36.],
   [   7.,   49.],
   [   8.,   64.],
   [   9.,   81.],
   [  10.,  100.]]
line(DATA,marker='^',linestyle='')

【讨论】:

以上是关于ValueError:使用 Sage 绘图时“解包的值太多”的主要内容,如果未能解决你的问题,请参考以下文章

在训练 Bert 二进制分类模型时,Huggingface 变形金刚返回“ValueError:要解包的值太多(预期为 2)”

OpenCV python:ValueError:解包的值太多

如何修复ValueError:没有足够的值来解包(预期2,得到1)[重复]

Tensorboard - ValueError:解包的值太多(预期 2)

ValueError:在 Pandas 数据帧上使用 itertuples() 时解包的值太多

Python:ValueError:需要多于0个值才能解包