[英]ValueError: too many values to unpack (expected 2) when plotting a 2dhistogram
[英]ValueError: “too many values to unpack” when plotting with Sage
到目前為止,這是我的代碼。 我有Python 2.7(我今天和Sage一起下載)並且正在從終端在Mac OS X 10.6.8上運行它(但是,我確實計划從筆記本()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'
使用list_plot
。 它的文件說
如果您有相互映射的x值和y值的單獨列表,請使用“zip”命令創建一個列表,其條目是(x,y)值對,並將結果提供給“list_plot” “
所以特別要使用list_plot(zip(X,Y))
。 或者,將DATA
轉換為單個列表:使用list_plot(DATA.tolist())
。
我還指出我喜歡使用帶有linestyle=''
選項的line()
,所以我可以獲得更多的matplotlib選項(由於某種原因, list_plot
和points
不能很好地完成這個):
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='')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.