Fourier series animation effect using by python ArtistAnimation - python

I'm studying about Fourier Series with python.
I drew it with cosine and sine function.
My code is like this.
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
x = Symbol('x')
fx=0
j=10
for i in range(1,j):
fx=fx+(2)/(np.pi*i)*(1-cos(i*np.pi))*(sin(i*x))
y_func=lambdify(x, fx, "numpy")
x_val=np.linspace(-np.pi,np.pi,315)
y_val=y_func(x_val)
plt.plot(x_val,y_val)
plt.show()
I could get the correct graph.
And I tried to make the graph into animaion effect like this gif file.
enter image description here
I wrote the code like below using by ArtistAnimation , but I couldn't get the animation.
How can i get the animation?
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
from matplotlib.animation import ArtistAnimation
x = Symbol('x')
fx=0
j=10
img=[]
fig, ax=plt.subplots(constrained_layout=True)
for i in range(1,j):
fx=fx+(2)/(np.pi*i)*(1-cos(i*np.pi))*(sin(i*x))
y_func=lambdify(x, fx, "numpy")
x_val=np.linspace(-np.pi,np.pi,315)
y_val=y_func(x_val)
fs=plt.plot(x_val,y_val)
img.append([fs])
anim=ArtistAnimation(fig,img,interval=5)
anim.save("Fourier_Series01.gif",fps=24)
I tried to make the code with ArtistAnimation
Thank you for your answer

Related

Is there a way in numpy/matplotlib etc to get the function of a graph?

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model(y,t):
dydt= 0.6*(2.11-y)-1.17*(4.767-4.767*np.exp(-1.4*y))
return dydt
y0=0
t = np.linspace(0,10)
y = odeint(model,y0,t)
plt.plot(t,y)
plt.xlabel("t")
plt.ylabel("S")
plt.show()
This code produces a graph, the need the equation/function of it. Is there any way of getting it ??
I have searched many times, but theres no answers to this kind of questions.

How to make minimum point can see from above of surface plot?

I want to plot the surface graph and minimum point as follows:
I have tried to make python code:
import sympy
import numpy
from numpy import linalg
from numpy import linspace
from sympy import lambdify
import matplotlib.pyplot as plt
from matplotlib import cm
x1,x2=sympy.symbols('x1 x2')
f=x1**2-x1*x2-4*x1+x2**2-x2
lam_f = lambdify([x1,x2], f, modules=['numpy'])
x1value=linspace(-6,6,50)
x2value=linspace(-6,6,50)
x1value, x2value = numpy.meshgrid(x1value, x2value)
fvalue=lam_f(x1value,x2value)
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(x1value, x2value, fvalue, cmap=cm.jet,linewidth=0, antialiased=False,label="$f(x_1,x_2)$")
ax.set_xlabel('$x_1$')
ax.set_ylabel('$x_2$')
ax.set_zlabel('$f(x_1,x_2)$')
ax.scatter(3,2,-7,color="cyan",s=50,marker="s",zorder=2,linewidths=2)
plt.show()
The minimum point cannot see from above, but from below we can see the minimum point.
I want minimum point can see from above as the first picture. How to make it?

numpy and matplot - plotting on the same graph while one element changes

I'm trying to create a graph with k_b as the x-value and delta_P as the y-value. I want to plot k_b against delta_P but S=3 for one curve and S=0.1 for another curve. However, I want the two lines to be on the same graph. Does anyone have any advice on how to do that? Below is what I have for S=3 and it works.
def rocproduct(k_cat,E0,S,k_b,k_f):
return k_cat*E0*S/((k_b/k_f)+S)
import numpy as np
import matplotlib.pyplot as plt
k_cat=0.1;E0=1;k_f=0.3;S=3
k_b=np.array([0.01,0.1,0.2,0.5,1,1.5,2,5,10])
delta_P=rocproduct(k_cat,E0,S,k_b,k_f)
plt.ylabel('rate of change of product')
plt.xlabel('kb')
plt.plot(k_b,delta_P)
Just call rocproduct for S=0.1 and plot it again
import numpy as np
import matplotlib.pyplot as plt
# Parameters
k_cat=0.1
E0=1
k_f=0.3
S=3
# Function for data
def rocproduct(k_cat,E0,S,k_b,k_f):
return k_cat*E0*S/((k_b/k_f)+S)
# Data to plot
k_b=np.array([0.01,0.1,0.2,0.5,1,1.5,2,5,10])
delta_P_1=rocproduct(k_cat,E0,S,k_b,k_f)
S = 0.1
delta_P_2=rocproduct(k_cat,E0,S,k_b,k_f)
# Plotting
plt.ylabel('rate of change of product')
plt.xlabel('kb')
plt.plot(k_b,delta_P_1)
plt.plot(k_b, delta_P_2)
plt.show()

How does `matplotlib` adjust plot to figure size?

How does matplotlib ensure that a dataset can be within plot with specified size.
How do i from a plot stored as numpy, How do i read the color of the pixels illustration a datapoint (0,4) - in the plot.
example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")
First plot being :
Second plot being:
so the first has been resized to plot size 12,4 where the last basically plots the same data but just using the data shape as size... how do i change that?
Librosa just performs pcolormesh according to the GitHub source code
You need to define another figure with its own size for the second figure.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
fig = plt.figure(figsize=(12,4))
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")

How to bias an absolute sine wave in Python

I am using the below python code so as to bias an absolute sine wave. I would like to have only the crest part of the wave and not the trough part even after positive biasing.Here I am unable achieve continuous crest signal after positive biasing. Can any one help me in this?
Usage: Keeping the input signals above the threshold even during dynamic shift of threshold.
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x=np.linspace(-20,20,1000);
y=np.abs(np.sin(x)+Bias);
#Bias=np.zeros_like(x); # This is not working
y[(y<=Bias)]= Bias + y # This is not working
plt.plot(x,y)
plt.grid()
plt.show()
It is a litle bit unclear what you are asking... Maybe you want to try this:
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x = np.linspace(-20, 20, 1000);
y = np.abs(np.sin(x))
y = y + Bias
plt.plot(x, y)
plt.grid()
plt.show()
or this:
import matplotlib.pyplot as plt
import numpy as np
Bias=5;
x=np.linspace(-20,20,1000);
y=np.abs(np.sin(x) + Bias);
y[(y<=Bias)]= Bias
plt.plot(x,y)
plt.grid()
plt.show()

Categories

Resources