Created
January 30, 2013 00:51
-
-
Save jseabold/4669598 to your computer and use it in GitHub Desktop.
draw a spiral in matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: Try using plt.show()
at the end.
It can't work
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
Nothing happens. No plot appears.