Matplotlib 没有显示任何内容
Matplotlib not showing anything
我想使用此代码在 macosx 上制作频谱分析仪流。
但没有显示任何东西。 matplotlib 打开。谁能帮我解决问题?
python 3.8.0
matplotlib 3.1.1
音频 0.2.11
麻木 1.17.3
scipy1.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')
我想使用此代码在 macosx 上制作频谱分析仪流。 但没有显示任何东西。 matplotlib 打开。谁能帮我解决问题? python 3.8.0 matplotlib 3.1.1 音频 0.2.11 麻木 1.17.3 scipy1.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')