/    Sign up×
Bounties /Pin to ProfileBookmark

will the following code create cymatics?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

def init():
    data = np.zeros((100, 100))
    ax.imshow(data, cmap='gray')
    return ax

def update(frame):
    data = np.sin(frame / 10.0)
    ax.imshow(data, cmap='gray')
    return ax

ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)
plt.show()

to post a answer
Python

5 Replies

Copy linkTweet thisAlerts:
@bbttFeb 09.2023 — You have a few issues here. First and foremost is that this code won't run since you're passing a AxesSubplot to FuncAnimation instead of a Figure object.

The following code was written with a bit of help from GPT and accomplishes what I believe you're looking for
``
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

def init():
global im
data = np.zeros((100, 100))
im = ax.imshow(data, cmap='gray', vmin=0, vmax=1)
return [im]

def update(frame):
global im
data = np.sin(np.linspace(0, 2 * np.pi, 100) + frame / 10.0)
data = np.outer(data, data)
data = np.interp(data, (data.min(), data.max()), (0, 1))
im.set_data(data)
return [im]

ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)
plt.show()
``
Copy linkTweet thisAlerts:
@OusseemFeb 13.2023 — No, this code will not create cymatics. This code will create a simple animation that shows a sine wave changing over time, but cymatics typically involves the visualization of sound waves or vibrations as patterns in a medium such as sand or liquid. The code you posted generates an animation of a 2D sine wave in grayscale, but does not have the functionality to produce cymatics.
Copy linkTweet thisAlerts:
@steimeFeb 13.2023 — No, this code does not create cymatics. It creates an animation using the sine function that updates 100 times. The animation shows a grayscale image with values ranging from -1 to 1, representing the sine wave's amplitude over time. This animation may visualize the behavior of sound waves, but it does not create cymatics in the traditional sense, as it does not demonstrate the physical patterns formed by sound waves in a medium.

Here is an example of code in Python that creates cymatics:


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

def init():
data = np.zeros((100, 100))
ax.imshow(data, cmap='gray')
return ax

def update(frame):
data = np.zeros((100, 100))
f = frame / 10.0
for x in range(100):
for y in range(100):
value = np.sin(np.sqrt(x**2 + y**2) + f)
data[x, y] = value
ax.imshow(data, cmap='gray')
return ax

ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)
plt.show()


This code creates an animation of a 2D surface that changes over time, where each point on the surface represents the magnitude of a sine wave with a unique frequency determined by its distance from the center. This effectively visualizes the behavior of sound waves in a medium and can be considered an example of creating cymatics.
Copy linkTweet thisAlerts:
@OnlineDevelopersFeb 12.2023 — import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

def init():
data = np.zeros((100, 100))
ax.imshow(data, cmap='gray')
return ax

def update(frame):
x = np.linspace(0, 2 * np.pi, 100)
y = np.linspace(0, 2 * np.pi, 100)
X, Y = np.meshgrid(x, y)
data = np.sin(np.sqrt((X - frame / 10.0)**2 + Y**2))
ax.imshow(data, cmap='gray')
return ax

ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)
plt.show()
Copy linkTweet thisAlerts:
@OnlineDevelopersFeb 12.2023 — No, the code will not create cymatics. The code is a simple implementation of an animated sine wave plot using the NumPy and Matplotlib libraries in Python. It creates a 100x100 grayscale image that displays a sine wave animation with 100 frames, with the sine wave's amplitude changing in each frame. The plot is shown using the "plt.show()" function. The "update" function calculates the sine wave's amplitude based on the current frame, and the "init" function sets the initial state of the plot. The "blit" parameter of the "FuncAnimation" function is set to True, which is a method of optimizing the animation's performance by only updating the parts of the plot that have changed.
@OnlineDevelopersimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig, ax = plt.subplots() def init(): data = np.zeros((100, 100)) ax.imshow(data, cmap='gray') return ax def update(frame): x = np.linspace(0, 2 * np.pi, 100) y = np.linspace(0, 2 * np.pi, 100) X, Y = np.meshgrid(x, y) data = np.sin(np.sqrt((X - frame / 10.0)**2 + Y**2)) ax.imshow(data, cmap='gray') return ax ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True) plt.show()Feb 12.2023
×

Success!

Help @sdussias spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...