Skip to content

Instantly share code, notes, and snippets.

@jseabold
Created January 30, 2013 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jseabold/4669598 to your computer and use it in GitHub Desktop.
Save jseabold/4669598 to your computer and use it in GitHub Desktop.
draw a spiral in matplotlib
import numpy as np
import matplotlib.pyplot as plt
#As a path collection
from matplotlib.collections import LineCollection
fig, ax = plt.subplots()
r = np.arange(0, .075, 0.00001)
nverts = len(r)
theta = np.array(range(nverts)) * (2*np.pi)/(nverts-1)
theta = 90*np.pi*r
xoffset, yoffset = .5, .5
yy = 1.4*r * np.sin(theta) + yoffset
xx = r * np.cos(theta) + xoffset
spiral = zip(xx,yy)
collection = LineCollection([spiral], colors='k')
ax.add_collection(collection)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
# In polar coordinates
fig, ax = plt.subplots(subplot_kw=dict(polar=True, axisbg='none'), figsize=(.5,.5))
r = np.arange(0, 3.4, 0.01)
theta = 2*np.pi*r
ax.set_axis_off()
ax.grid(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.plot(theta, r, linewidth=2, color='k');
@greninja
Copy link

greninja commented Jan 2, 2017

Nothing happens. No plot appears.

@r-barnes
Copy link

@greninja: Try using plt.show() at the end.

@Sheldontao
Copy link

It can't work

@zjoels
Copy link

zjoels commented Apr 14, 2020

For python 3.7.6, make the following changes (in addition to plt.show() mentioned above):
Line 17 cast the zip iterator object with [list(spiral)] instead of just [spiral].
Line 24 replace axisbg with facecolor
Thanks for the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment