试图绘制傅立叶正弦曲线
Trying to plot Fourier sines
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()
当 运行 代码出现错误 AttributeError: 'Line2D' object has no 属性'markers ,'LineWidth'
如果我不使用标记和 LineWidth 代码运行但预期结果不是我想要的
我得到了大约 15 个图表,但这不是我想要的颜色样式也没有得到应用
AttributeError: 'Line2D' object has no property 'markers ,'LineWidth'
那是因为它应该是 marker
和 linewidth
:
进行这些更改,并使 plt.show()
脱离循环:
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()
...我们得到:
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()
当 运行 代码出现错误 AttributeError: 'Line2D' object has no 属性'markers ,'LineWidth' 如果我不使用标记和 LineWidth 代码运行但预期结果不是我想要的 我得到了大约 15 个图表,但这不是我想要的颜色样式也没有得到应用
AttributeError: 'Line2D' object has no property 'markers ,'LineWidth'
那是因为它应该是 marker
和 linewidth
:
进行这些更改,并使 plt.show()
脱离循环:
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()
...我们得到: