0

I have written a code with librosa module, to generate spectrogram of an audio file and other works. Apparently it works fine with Spyder where the output is generated after taking a while, but for PyCharm, it just gets executed. No output spectrogram.

I am fairly new to PyCharm environment please help.

My code is:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

audio_name = 'C:\\Users\\USER\\Desktop\\Songs\\abc.wav'
hop_length = 512
window_size = 2048

import librosa
y, sr = librosa.load(audio_name)    #y=time series(one-dimensional NumPy floating point array), sr= sampling rate of y, that is, the number of samples per second of audio. By default, all audio is mixed to mono and resampled to 22050 Hz at load time
window = np.hanning(window_size)
out  = librosa.core.spectrum.stft(y, n_fft = window_size, hop_length = hop_length, window=window)
out = 2 * np.abs(out) / np.sum(window)

import librosa.display
librosa.display.specshow(librosa.amplitude_to_db(out, ref=np.max), y_axis='log', x_axis='time')

''' To store spectrogram
from matplotlib.backends.backend_agg import FigureCanvasAgg
fig = plt.figure()
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot(111)
p = librosa.display.specshow(librosa.amplitude_to_db(out, ref=np.max), ax=ax, y_axis='log', x_axis='time')
fig.savefig('C:\\Users\\USER\\Desktop\\Songs\\spec.png')
'''

onset_env = librosa.onset.onset_strength(y=y, sr=sr, aggregate=np.median)
tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr)
fig, ax = plt.subplots(nrows=2, sharex=True)
times = librosa.times_like(onset_env, sr=sr, hop_length=hop_length)
M = librosa.feature.melspectrogram(y=y, sr=sr, hop_length=hop_length)
librosa.display.specshow(librosa.power_to_db(M, ref=np.max), y_axis='mel', x_axis='time', hop_length=hop_length, ax=ax[0])
ax[0].label_outer()
ax[0].set(title='Mel spectrogram')
ax[1].plot(times, librosa.util.normalize(onset_env), label='Onset strength')
ax[1].vlines(times[beats], 0, 1, alpha=0.5, color='r', linestyle='--', label='Beats')
ax[1].legend()

The output in Spyder (After taking a bit of time):

enter image description here

enter image description here

The output of PyCharm after execution:

enter image description here

Update: Apparently it works when I give a plt.show() at the end as referred by @Mandera. But can someone tell me why it works in Spyder even without this?

  • 1
    Maybe you just need to call `plt.show()` in the end? – Mandera Jul 30 '22 at 04:39
  • Thanks a lot @Mandera. It worked. But can you please tell me why it works for Spyder without plt.show() ? – Surya Majumder Jul 30 '22 at 04:43
  • 1
    Spyder probably has some built-in quality of life code, I haven't used it myself so it was just a guess. Glad it worked! – Mandera Jul 30 '22 at 04:44
  • 1
    PyCharm is just a (pretty good) IDE, while Spyder tries to be many more things. Although that may be convenient for users that do their work there, it can be confusing to Python developers that want to write scripts that will work anywhere / in many places. Your question basically comes down to the reverse of this: https://stackoverflow.com/questions/45680211/plt-show-not-working-in-spyder-ide – Grismar Jul 30 '22 at 04:49

0 Answers0