如何从 Matplotlib specgram 中删除图例?
How to remove legend from Matplotlib specgram?
我正在尝试删除频谱图的图例(我正在尝试获取 244x244 像素的图像)
我试过 Remove the legend on a matplotlib figure 但它的工作方式很奇怪 - 我得到的结果 和 是一个异常!看 -
图片 -
(我正在使用 google colab)
这是我使用的代码
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import moviepy.editor as mpy
import youtube_dl
## downloading the video
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=5pIpVK3Gecg'])
## selecting the audio clip from 17oth second to 180th second and saving it in talk.wav
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("Tyler, The Creator - 'IGOR,' Odd Future and Scoring a"
"Number 1 Album _ Apple Music-5pIpVK3Gecg.wav", 170,
180, targetname="talk.wav")
talk = mpy.AudioFileClip('talk.wav')
# switching axis off
plt.axis('off')
sample_rate = talk.fps
NFFT = sample_rate /25
audio_data = talk.to_soundarray()
#trying to get a 244 x 244 pixel image
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(2.44, 2.44), dpi=100.)
ax.axis('off')
spectrum, freqs, time, im = ax.specgram(audio_data.mean(axis=1), NFFT=NFFT, pad_to=4096,
Fs=sample_rate, noverlap=512, mode='magnitude', )
########## the problem lies here ##########
ax.get_legend.remove()
##trying to save stuff to drive but this doesnt run because of the exception
fig.colorbar(im)
fig.savefig('specto.png')
import os
print( os.getcwd() )
print( os.listdir('specto.png') )
from google.colab import files
files.download( "specto.png" )
有办法解决这个问题吗?
ax.get_legend
指函数对象本身,ax.get_legend()
(注意括号)调用函数,returns一个matplotlib.legend.Legend
实例或None
。语法应为
ax.get_legend().remove()
但是,如果没有图例被添加到情节中,这将引发
AttributeError: NoneType
object has no attribute remove
所以最好用try
和except
子句保护它,除非你提前知道会有传说。
编辑
根据评论,您混淆了 matplot.pyplot.colorbar()
with matplotlib.pyplot.legend()
,因此当在行
之前引发异常时,为什么它 "appears to work"
fig.colorbar(img)
它根本不绘制颜色条,因为它没有到达那条线。如果你不想要一个颜色条然后删除行
ax.get_legend().remove()
fig.colorbar(im)
你会得到一个没有颜色条的频谱图。
我正在尝试删除频谱图的图例(我正在尝试获取 244x244 像素的图像)
我试过 Remove the legend on a matplotlib figure 但它的工作方式很奇怪 - 我得到的结果 和 是一个异常!看 -
图片 -
(我正在使用 google colab)
这是我使用的代码
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import moviepy.editor as mpy
import youtube_dl
## downloading the video
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=5pIpVK3Gecg'])
## selecting the audio clip from 17oth second to 180th second and saving it in talk.wav
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("Tyler, The Creator - 'IGOR,' Odd Future and Scoring a"
"Number 1 Album _ Apple Music-5pIpVK3Gecg.wav", 170,
180, targetname="talk.wav")
talk = mpy.AudioFileClip('talk.wav')
# switching axis off
plt.axis('off')
sample_rate = talk.fps
NFFT = sample_rate /25
audio_data = talk.to_soundarray()
#trying to get a 244 x 244 pixel image
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(2.44, 2.44), dpi=100.)
ax.axis('off')
spectrum, freqs, time, im = ax.specgram(audio_data.mean(axis=1), NFFT=NFFT, pad_to=4096,
Fs=sample_rate, noverlap=512, mode='magnitude', )
########## the problem lies here ##########
ax.get_legend.remove()
##trying to save stuff to drive but this doesnt run because of the exception
fig.colorbar(im)
fig.savefig('specto.png')
import os
print( os.getcwd() )
print( os.listdir('specto.png') )
from google.colab import files
files.download( "specto.png" )
有办法解决这个问题吗?
ax.get_legend
指函数对象本身,ax.get_legend()
(注意括号)调用函数,returns一个matplotlib.legend.Legend
实例或None
。语法应为
ax.get_legend().remove()
但是,如果没有图例被添加到情节中,这将引发
AttributeError:
NoneType
object has no attributeremove
所以最好用try
和except
子句保护它,除非你提前知道会有传说。
编辑
根据评论,您混淆了 matplot.pyplot.colorbar()
with matplotlib.pyplot.legend()
,因此当在行
fig.colorbar(img)
它根本不绘制颜色条,因为它没有到达那条线。如果你不想要一个颜色条然后删除行
ax.get_legend().remove()
fig.colorbar(im)
你会得到一个没有颜色条的频谱图。