Fix Matplotlib tostring_rgb() removal

AttributeError: 'FigureCanvasAgg' object has no attribute 'tostring_rgb'
This commit is contained in:
Myungchul Keum 2025-02-04 09:54:27 +09:00 committed by GitHub
parent 7ef1986778
commit f0b67bd159
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -225,7 +225,6 @@ def plot_spectrogram_to_numpy(spectrogram):
mpl_logger = logging.getLogger("matplotlib") mpl_logger = logging.getLogger("matplotlib")
mpl_logger.setLevel(logging.WARNING) mpl_logger.setLevel(logging.WARNING)
import matplotlib.pylab as plt import matplotlib.pylab as plt
import numpy as np
fig, ax = plt.subplots(figsize=(10, 2)) fig, ax = plt.subplots(figsize=(10, 2))
im = ax.imshow(spectrogram, aspect="auto", origin="lower", interpolation="none") im = ax.imshow(spectrogram, aspect="auto", origin="lower", interpolation="none")
@ -235,8 +234,7 @@ def plot_spectrogram_to_numpy(spectrogram):
plt.tight_layout() plt.tight_layout()
fig.canvas.draw() fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep="") data = np.asarray(fig.canvas.buffer_rgba())
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
plt.close() plt.close()
return data return data
@ -251,7 +249,6 @@ def plot_alignment_to_numpy(alignment, info=None):
mpl_logger = logging.getLogger("matplotlib") mpl_logger = logging.getLogger("matplotlib")
mpl_logger.setLevel(logging.WARNING) mpl_logger.setLevel(logging.WARNING)
import matplotlib.pylab as plt import matplotlib.pylab as plt
import numpy as np
fig, ax = plt.subplots(figsize=(6, 4)) fig, ax = plt.subplots(figsize=(6, 4))
im = ax.imshow( im = ax.imshow(
@ -266,8 +263,7 @@ def plot_alignment_to_numpy(alignment, info=None):
plt.tight_layout() plt.tight_layout()
fig.canvas.draw() fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep="") data = np.asarray(fig.canvas.buffer_rgba())
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
plt.close() plt.close()
return data return data