试图绘制傅立叶罪

发布于 2025-01-24 04:56:25 字数 1329 浏览 1 评论 0原文

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'对象没有属性标记,'lineWidth' 如果我不使用标记和线宽,则代码运行,但预期的结果不是我想要的 我得到了大约15个图,但这不是我想要的色彩样式也没有应用

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2025-01-31 04:56:25

attributeError:'line2d'对象没有属性'标记,'lineWidth'

是因为它应该是标记line> lineWidth

随着这些更改,并进行plt.showshow ()脱离循环:

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()

...我们获得:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文