如何使用numpy和matplotlib绘制单位圆

dum*_*umm 1 geometry numpy matplotlib

我想使用numpy和matplotlib绘制一个单位圆(cos + sin)。我写了以下内容:

t = np.linspace(0,np.pi*2,100)
circ = np.concatenate((np.cos(t),np.sin(t)))
Run Code Online (Sandbox Code Playgroud)

我策划了,但是失败了。

ax.plot(t,circ,linewidth=1)
ValueError: x and y must have same first dimension
Run Code Online (Sandbox Code Playgroud)

War*_*ser 5

plot不做参数图。您必须给它xy值,而不是t

xcos(t)ysin(t),因此将这些数组赋给plot

ax.plot(np.cos(t), np.sin(t), linewidth=1)
Run Code Online (Sandbox Code Playgroud)


Jul*_*nck 5

或者您可以使用 Circle (http://matplotlib.org/api/patches_api.html):

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
circ = plt.Circle((0, 0), radius=1, edgecolor='b', facecolor='None')
ax.add_patch(circ)
plt.show()
Run Code Online (Sandbox Code Playgroud)