python - Plotting a polygon with matplotlib, error with vertices -
i'm trying plot polygon x^11+1=0, , i've got error matrix's dimension. matrix elements polygon's vertices. code:
import numpy np import matplotlib.pyplot plt matplotlib.collections import polycollection n = 11 verticesx = np.empty(shape=0) 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)
the error message one:
[-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 exit code 1
import numpy np __future__ import division import matplotlib.pyplot plt matplotlib.collections import polycollection n = 11 verts = [] k=np.linspace(0,n,1.0) 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 numpy array works differently appending 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) # need pass collection here, not 1 polygon, solve 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()
Comments
Post a Comment