Home > database >  Trying to plot Fourier sines
Trying to plot Fourier sines

Time:04-27

from matplotlib import markers
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap

plt.rcParams['figure.figsize'] = [9,9]
plt.rcParams.update({'font.size' : 16})

#domain definition
dx = 0.01 #input("input the step size: ")
x = np.pi*np.arange(-1  float(dx),1 float(dx),float(dx))
n = len(x)
nquart = int(np.floor(n/4))

#hat funtion
f = np.zeros_like(x)
f[nquart : 2*nquart] = (4/n)*np.arange(1,nquart 1)
f[2*nquart:3*nquart] = np.ones(nquart) - (4/n)*np.arange(0,nquart)

#subplot creation
fig,ax = plt.subplots(1)
ax.plot(x,f)

#core fourier series
name = 'accent'
cmap = get_cmap('tab10')
colors = cmap.colors
ax.set_prop_cycle(color = colors)

# sum of values with an array of ones with the same shape and type as a given array.
Ao = np.sum(f*np.ones_like(x))*dx
ffs = Ao/2

A = np.zeros(20)
B = np.zeros(20)

for k in range(20):
    #the inner products
    A[k] = np.sum(f*np.cos(np.pi*(k 1)*(x/np.pi)))*dx
    B[k] = np.sum(f*np.sin(np.pi*(k 1)*(x/np.pi)))*dx

    ffs = ffs   A[k]*np.cos((k 1)*np.pi*(x/np.pi))   B[k]*np.sin((k 1)*np.pi*(x/np.pi))
    ax.plot(x,ffs,markers = 'o',LineWidth = 1.5)
    plt.show()

when running the code i get an error AttributeError: 'Line2D' object has no property'markers ,'LineWidth' if i do not use the markers and LineWidth the code runs but the expected outcome is not what i wanted i am getting around 15 graphs but that is not what i wanted the color style is also not getting applied

CodePudding user response:

AttributeError: 'Line2D' object has no property 'markers ,'LineWidth'

That's because it should be marker and linewidth:

With those changes, and taking plt.show() out of the loop:

for k in range(20):
    #the inner products
    A[k] = np.sum(f*np.cos(np.pi*(k 1)*(x/np.pi)))*dx
    B[k] = np.sum(f*np.sin(np.pi*(k 1)*(x/np.pi)))*dx

    ffs = ffs   A[k]*np.cos((k 1)*np.pi*(x/np.pi))   B[k]*np.sin((k 1)*np.pi*(x/np.pi))
    ax.plot(x,ffs,marker = 'o',linewidth = 1.5)
    
plt.show()

...we obtain: enter image description here

  • Related