QuadMesh 对象没有属性图?

QuadMesh object has no attribute plot?

使用频谱图作为基础计算频谱特征给出错误'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'

期望的输出

您已经在使用

创建子图

fig, ax = plt.subplots(figsize=(10, 5))

所以不用

ax = specshow(spec_db, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)

你应该传入子图(Axes)来使用。

specshow(spec_db, ax=ax, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)