On the images below, the same audio file is represented. On the left side, there's a correct, original and NOT NORMALIZED audio preview.
I want to fix the following problems of the matplotlib's graph:
Correct the audio level (amplitude)
- Stop truncating the height
- There's small space at Y-axis
- I've tested normalized files with different sample rates - this padding occurs regardless of audio level
| Correct preview | Matplotlib |
|---|---|
![]() |
![]() |
Code I've used:
import numpy as np
import wave, sys, os
import matplotlib.pyplot as plt
File_Name = os.path.basename(sys.argv[1])
plt.rcParams["axes.xmargin"] = 0
#-=-=-=-#
try:
Audio = wave.open(sys.argv[1], "r")
except Exception:
raise SystemExit("WAV format not integer, exiting.")
if Audio.getnchannels() > 1:
raise SystemExit("Not a mono file, exiting.")
Signal = Audio.readframes(-1)
Signal = np.fromstring(Signal, "int16")
Time = np.linspace(0, len(Signal) / Audio.getframerate(), num = len(Signal))
#-=-=-=-#
plt.plot(Time, Signal, color = "#000000")
#plt.axis("off")
#plt.savefig(os.path.splitext(File_Name)[0] + ".png",
# bbox_inches = "tight", transparent = 1, pad_inches = 0
#)
plt.show()

