【发布时间】:2021-06-22 19:58:19
【问题描述】:
使用光谱图作为基础计算光谱特征会产生错误'QuadMesh' object has no attribute 'plot'
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import librosa as lr
from librosa.core import stft, amplitude_to_db
from librosa.display import specshow
HOP_LENGTH = 2**4
SIZE_WINDOW = 2**7
# For the test
spec = pd.read_csv('spec.csv', index_col=0)
spec = np.array(np.abs(spec))
time = np.array(normal.index)
audio = pd.read_csv('audio.csv', index_col=0).to_numpy().squeeze()
sfreq = 2205
# Calculate the spectral centroid and bandwidth for the spectrogram
bandwidths = lr.feature.spectral_bandwidth(S=spec)[0]
centroids = lr.feature.spectral_centroid(S=spec)[0]
# Convert spectrogram to decibels for visualization
spec_db = amplitude_to_db(spec)
# Display these features on top of the spectrogram
fig, ax = plt.subplots(figsize=(10, 5))
ax = specshow(spec_db, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)
ax.plot(times_spec, centroids)
ax.fill_between(times_spec, centroids - bandwidths / 2, centroids + bandwidths / 2, alpha=.5)
ax.set(ylim=[None, 6000])
plt.show()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-896743695f65> in <module>()
15 fig, ax = plt.subplots(figsize=(10, 5))
16 ax = specshow(spec_db, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)
---> 17 ax.plot(times_spec, centroids)
18 ax.fill_between(times_spec, centroids - bandwidths / 2, centroids + bandwidths / 2, alpha=.5)
19 ax.set(ylim=[None, 6000])
AttributeError: 'QuadMesh' object has no attribute 'plot'
期望的输出
【问题讨论】:
标签: python-3.x matplotlib machine-learning librosa