I want to plot two sinusoidal functions. The generation of the plotting should be done according to the pixel size, like pixel by pixel. Any help regarding how to do this? The code I used to generate the waveform[ image attached in below]is below:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
amp = 0.5 # 1V (Amplitude)
f = 20
frame_rate = 100
t = np.linspace(0, np.pi/2, 1000)
x = np.sin(t)
y = amp * (np.cos(2 * np.pi * f * t) + np.pi / 2)
# pixel size in inches (width,height)
dpi = 64
fig, ax = plt.subplots(figsize=(4, 4), dpi=dpi)
line, = ax.step(x, y)
plt.show()
def animate(frame, x, y, line):
line.set_data(x[:frame], y[:frame])
y_new = 0.5 * (np.cos(2 * np.pi * 20 * t[:frame]/2) + np.pi / 2)
line.set_ydata(y_new)
return line,
interval = 1000 / frame_rate
ani = FuncAnimation(fig, animate, frames=1000, interval=interval, fargs=[x, y, line])
HTML(ani.to_jshtml())
I thought to use the OpenCV library for this purpose. Is there any other easier option? Any help with codes, explanations and providing resources would be highly appreciated.
Here's a small example of how to plot a function with frame-varied parameters using Pillow, upscaling it 2x using nearest-neighbor interpolation so you can really see the pixels.
It saves the resulting animation as an animated GIF.
For this code, the animation result (sans the upscaling to save SO's bandwidth) is: