dumm
dumm

Reputation: 53

How to draw an unit circle using numpy and matplotlib

I want to draw an unit circle(cos+sin), using numpy and matplotlib. I wrote the following:

t = np.linspace(0,np.pi*2,100)
circ = np.concatenate((np.cos(t),np.sin(t)))

and I plotted, but failed.

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

Upvotes: 2

Views: 16525

Answers (2)

Julien Spronck
Julien Spronck

Reputation: 15433

Or you can use 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()

Upvotes: 5

Warren Weckesser
Warren Weckesser

Reputation: 114811

plot does not do a parametric plot. You must give it the x and y values, not t.

x is cos(t) and y is sin(t), so give those arrays to plot:

ax.plot(np.cos(t), np.sin(t), linewidth=1)

Upvotes: 6

Related Questions