Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib打印多边形,顶点错误_Python_Python 3.x_Numpy_Matplotlib - Fatal编程技术网

Python 使用matplotlib打印多边形,顶点错误

Python 使用matplotlib打印多边形,顶点错误,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,我试图为x^11+1=0绘制一个多边形,但矩阵的维数有一个错误。矩阵元素是多边形的顶点。守则: import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import PolyCollection n = 11 verticesx = np.empty(shape=0) for k in np.arange(n): verticesx = np.append(verticesx, [[

我试图为x^11+1=0绘制一个多边形,但矩阵的维数有一个错误。矩阵元素是多边形的顶点。守则:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection


n = 11
verticesx = np.empty(shape=0)
for k in np.arange(n):
    verticesx = np.append(verticesx, [[np.cos(np.rad2deg((2 * k + 1) / n * np.pi))],
                                      [np.sin(np.rad2deg((2 * k + 1) / n * np.pi))]])
print(verticesx)
plt.subplot(aspect='equal')
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
circle = plt.Circle((0, 0), 1, color='b', fill=False)
poly = PolyCollection(verticesx, facecolor='r', edgecolor='g', closed=True)
plt.gcf().gca().add_artist(circle)
plt.gcf().gca().add_artist(poly)
错误消息如下:

[-0.79263773-0.6096929 0.38593669-0.92252527 0.99066117
0.13634679
0.12236983  0.99248457 -0.92787342  0.37289531 -0.59846007 -0.80115264
0.6208046  -0.78396533  0.91699383  0.39890139 -0.15029666  0.98864094      -0.99411079  0.10836856 -0.35977985 -0.93303722] 
回溯(最近一次调用上次):文件
“F:/MISCOSAS/programmaspython3/Circunferencia/circunferenciaunidad.py”,
第15行,在
poly=PolyCollection(verticesx,facecolor='r',edgecolor='g',closed=True)文件
“C:\Anaconda3\lib\site packages\matplotlib\collections.py”,第867行,
在初始化中__
self.set_verts(verts,closed)文件“C:\Anaconda3\lib\site packages\matplotlib\collections.py”,第878行,
在集合中
if len(xy):TypeError:numpy.float64类型的对象没有len()
进程已完成,退出代码为1

[-0.79263773 -0.6096929   0.38593669 -0.92252527  0.99066117 
0.13634679
  0.12236983  0.99248457 -0.92787342  0.37289531 -0.59846007 -0.80115264
  0.6208046  -0.78396533  0.91699383  0.39890139 -0.15029666  0.98864094      -0.99411079  0.10836856 -0.35977985 -0.93303722] 
Traceback (most recent call last):   File
"F:/MISCOSAS/ProgramasPython3/Circunferencia/circunferenciaunidad.py",
line 15, in <module>
    poly = PolyCollection(verticesx, facecolor='r', edgecolor='g', closed=True)   File
"C:\Anaconda3\lib\site-packages\matplotlib\collections.py", line 867,
in __init__
    self.set_verts(verts, closed)   File "C:\Anaconda3\lib\site-packages\matplotlib\collections.py", line 878,
in set_verts
    if len(xy): TypeError: object of type 'numpy.float64' has no len()

Process finished with exit code 1
import numpy as np
from __future__ import division
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection


n = 11
verts = []

k=np.linspace(0,n,1.0)

for k in range(n):
    a=360/11/180*np.pi
    x= np.cos( k*a ) 
    y= np.sin( k*a ) 
    verts.append((x,y))
    # appending to a numpy array works differently than appending to a list


plt.subplot(aspect='equal')
# plt.xlim(-1.5, 1.5)
# plt.ylim(-1.5, 1.5)
circle = plt.Circle((0, 0), 1, color='b', fill=False)

# you need to pass a collection here, not just one polygon, solve that by putting brackets around
poly = PolyCollection([verts], facecolor='r', edgecolor='g', closed=True)


plt.gcf().gca().add_artist(circle)
plt.gcf().gca().add_artist(poly)
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.show()