2016-06-21 22 views
3

我試圖獲得的圓的60行接合中心到60個不同的等間隔的點上在下面的程序中的圓周上,numpy的linspace和繪圖,ValueError異常:用序列

import matplotlib.pyplot as plt 
import numpy as np 

figure = plt.figure(figsize=(10, 10)) 

theta = np.linspace(0, 2 * np.pi, 60) 
r = 3.0 
x1 = r * np.cos(theta) 
y1 = r * np.sin(theta) 

plt.plot(x1, y1, color='blue') 
plt.plot([0, x1], [0, y1], color='gray') 

plt.axis([-4, 4, -4, 4]) 
plt.grid(True) 

figure.tight_layout() 
figure.savefig('test.png', facecolor='white', edgecolor='black') 

的數組元素它提供了以下錯誤,

$ python test.py 
Traceback (most recent call last): 
    File "test.py", line 12, in <module> 
    plt.plot([0, x1], [0, y1], color='gray') 
    File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2987, in plot 
    ret = ax.plot(*args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4137, in plot 
    for line in self._get_lines(*args, **kwargs): 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 317, in _grab_next_args 
    for seg in self._plot_args(remaining, kwargs): 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 288, in _plot_args 
    y = np.atleast_1d(tup[-1]) 
    File "/usr/lib/python2.7/dist-packages/numpy/core/shape_base.py", line 49, in atleast_1d 
    ary = asanyarray(ary) 
    File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 512, in asanyarray 
    return array(a, dtype, copy=False, order=order, subok=True) 
ValueError: setting an array element with a sequence. 

如果我使用了一些固定值,例如plt.plot([0, 0], [0, r], color='gray')而不是plt.plot([0, x1], [0, y1], color='gray')它的工作原理。這似乎與numpy.linspace這樣的情節是不可能的。

我發現類似的問題ValueError: setting an array element with a sequence,但並沒有幫助我。我是python的新手,請耐心等待。

回答

2

x和y的plot()命令的元素需要有相同數量的元素。用以下替換行

plt.plot([0, x1], [0, y1], color='gray') 

plt.plot([np.zeros(60,), x1], [np.zeros(60,), y1], color='gray') 

的結果是這樣的: enter image description here