gpt4 book ai didi

numpy - 属性错误: 'numpy.ndarray' object has no attribute '_hold'

转载 作者:行者123 更新时间:2023-12-01 16:39:09 32 4
gpt4 key购买 nike

我在Python3中使用numpy和matplotlib。
以下代码导致错误:

import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.axes import Subplot as plt
from matplotlib import pylab


a=[1,1]
b=[1,1]
fsam = 48000
w, h = freqz(b, a, worN=2000)
plt.plot(((fsam-8000) * 0.5 / np.pi) * w, abs(h), label=" ")

matplotlib 1.3.x 的实际错误:

 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.x-py3.2-linux-x86_64.egg/matplotlib/axes.py", line 4119, in plot   
if not self._hold:
AttributeError: 'numpy.ndarray' object has no attribute '_hold'

matplotlib 1.2.0 的实际错误:

 Traceback (most recent call last):   
File "/home/christoph/audio_measurement/AudioTools/AudioTools.py", line 222, in <module>
main()
File "/home/christoph/audio_measurement/AudioTools/AudioTools.py", line 216, in main
form = AppForm()
File "/home/christoph/audio_measurement/AudioTools/AudioTools.py", line 39, in __init__
self.on_draw()
File "/home/christoph/audio_measurement/AudioTools/AudioTools.py", line 80, in on_draw
self.transfer = Transfer(self.canvas)
File "/home/christoph/audio_measurement/AudioTools/Transfer.py", line 42, in __init__
plt.plot(((fsam-8000) * 0.5 / np.pi) * w, abs(h), label=" ")
File "/usr/local/lib/python3.2/dist-packages/matplotlib/axes.py", line 3995, in plot
if not self._hold: self.cla()
AttributeError: 'numpy.ndarray' object has no attribute '_hold'

Transfer 是一个类,它绘制在 Canvas 上。

我查看了系数a和b的长度,但它们并没有影响结果。
我找不到任何相关内容。有谁知道出了什么问题吗?

最佳答案

通常我会使用 import matplotlib.pyplot as pltplt.plotplt.subplotplt.show 等——甚至只是 from pylab import *。无论如何,这一行

from matplotlib.axes import Subplot as plt

是您有一个未绑定(bind)的 plot 函数尝试对 ndarray 参数进行操作的原因。 Subplot 需要实例化。这应该有效:

import numpy as np
from scipy.signal import freqz

import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.axes import Subplot

fig = Figure()
ax = Subplot(fig, 111)
fig.add_subplot(ax)
canvas = FigureCanvas(fig)

a=[1,1]
b=[1,1]
fsam = 48000
w, h = freqz(b, a, worN=2000)

ax.plot(((fsam-8000) * 0.5 / np.pi) * w, abs(h), label=" ")
canvas.show()

关于numpy - 属性错误: 'numpy.ndarray' object has no attribute '_hold' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15543681/

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