Skip to content

Instantly share code, notes, and snippets.

@dirediredock
Created May 2, 2022 21:57
Show Gist options
  • Select an option

  • Save dirediredock/3a0bee117b23eb769683c59b12113b4c to your computer and use it in GitHub Desktop.

Select an option

Save dirediredock/3a0bee117b23eb769683c59b12113b4c to your computer and use it in GitHub Desktop.
# by Matias I. Bofarull Oddo - 2022.05.02
# A small demo of sliders in matplotlib.pyplot
from math import pi, cos, sin
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
phi = (1 + (5 ** (1 / 2))) / 2 - 1
number_of_points = 1000
################################### SPIRALS ###################################
coord_x = []
coord_y = []
for i in range(number_of_points):
distance = i / (number_of_points)
angle = i * 2 * pi * phi
coord_x.append(distance * cos(angle))
coord_y.append(distance * sin(angle))
fig = plt.figure(figsize=(10, 10))
(Plot,) = plt.plot(coord_x, coord_y, "o", markersize=4)
plt.axis("off")
margin = 0.1
edge = max([max(coord_x), max(coord_y)])
plt.xlim(-edge - (margin * edge), edge + (margin * edge))
plt.ylim(-edge - (margin * edge), edge + (margin * edge))
plt.gca().set_position([0, 0.01, 1, 1])
ax_slider = plt.axes([0.02, 0.015, 0.9, 0.01])
N_slider = Slider(ax_slider, "", 0.5, 1, valinit=phi, valstep=0.00001)
def update(val):
turn_fraction = N_slider.val
coord_x = []
coord_y = []
for i in range(number_of_points):
distance = i / (number_of_points)
angle = i * 2 * pi * turn_fraction
coord_x.append(distance * cos(angle))
coord_y.append(distance * sin(angle))
Plot.set_xdata(coord_x)
Plot.set_ydata(coord_y)
fig.canvas.draw_idle()
N_slider.on_changed(update)
plt.show()
################################### DENSITY ###################################
coord_x = []
coord_y = []
for i in range(number_of_points):
distance = i / (number_of_points)
angle = i * 2 * pi * phi
coord_x.append(distance * cos(angle))
coord_y.append(distance * sin(angle))
fig = plt.figure(figsize=(10, 10))
(Plot,) = plt.plot(coord_x, coord_y, "o", markersize=4)
plt.axis("off")
margin = 0.1
edge = max([max(coord_x), max(coord_y)])
plt.xlim(-edge - (margin * edge), edge + (margin * edge))
plt.ylim(-edge - (margin * edge), edge + (margin * edge))
plt.gca().set_position([0, 0.01, 1, 1])
ax_slider = plt.axes([0.02, 0.015, 0.9, 0.01])
N_slider = Slider(ax_slider, "", 0, 2, valinit=1, valstep=0.001)
def update(val):
turn_fraction = N_slider.val
coord_x = []
coord_y = []
for i in range(number_of_points):
distance = (i / (number_of_points)) ** turn_fraction
angle = i * 2 * pi * phi
coord_x.append(distance * cos(angle))
coord_y.append(distance * sin(angle))
Plot.set_xdata(coord_x)
Plot.set_ydata(coord_y)
fig.canvas.draw_idle()
N_slider.on_changed(update)
plt.show()
###############################################################################
@dirediredock
Copy link
Author

Spirals.mov

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