gpt4 book ai didi

python - 试图绘制傅立叶正弦曲线

转载 作者:行者123 更新时间:2023-12-05 05:42:38 27 4
gpt4 key购买 nike

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 property'markers ,'LineWidth'如果我不使用标记和 LineWidth 代码运行但预期结果不是我想要的我得到了大约 15 个图表,但这不是我想要的颜色样式也没有得到应用

最佳答案

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

那是因为它应该是markerlinewidth:

有了这些改变,并使 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()

...我们得到: enter image description here

关于python - 试图绘制傅立叶正弦曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71994719/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com