【问题标题】:QuadMesh object has no attribute plot?QuadMesh 对象没有属性图?
【发布时间】: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


    【解决方案1】:

    您已经在创建一个子图

    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)
    

    【讨论】:

    • @Faizy:如果您更喜欢不使用 ax 的版本,而是使用 plt 和全局 matplotlib 状态,我建议对此做出另一个答案 - 因为这是完全不同的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-08-03
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多