我想使用此代码在macosx上创建频谱分析仪流。但什么也没显示。 matplotlib剂量打开。谁能帮我什么忙?python 3.8.0matplotlib 3.1.1PyAudio的0.2.11numpy的1.17.3scipy 1.3.1
import pyaudio
import struct
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
plt.show()
CHUNK = 1024*4
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 10000
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK
)
fig, ax = plt.subplots()
x = np.arange(0, 2* CHUNK, 2)
line, = ax.plot(x, np.random.rand(CHUNK))
ax.set_ylim(-256, 256)
ax.set_xlim(0, CHUNK)
while True:
data = stream.read(CHUNK)
data_int = np.array (struct.unpack(str(2*CHUNK)+ 'B', data), dtype='b')[::2]-128
line.set_ydata(data_int)
fig.canvas.draw()
fig.canvas.flush_events()
您必须定义要使用的matplotlib后端。将这两行相加即可完成工作:
import matplotlib
matplotlib.use('MacOSX')