I know there is a very similar question here. However, I tried all of the possible solutions posted there and absolutely none of them worked for me. This is why I'm creating this question.
This is what I have:
import matplotlib.pyplot as plt
from scipy.io import wavfile
sample_rate, samples = wavfile.read('audio-mono-70.wav')
fig = plt.figure(figsize=(6,1), dpi=500, frameon=False)
ax = plt.Axes(fig, [0,0,1,1])
ax.set_facecolor((0.169,0.169,0.169))
ax.set_xlim(left=0, right=700000)
fig.add_axes(ax)
plt.tick_params(axis='both', which='both', bottom=False,
top=False, left=False, right=False,
labelbottom=False, labeltop=False,
labelright=False, labelleft=False)
plt.plot(samples)
plt.savefig('samples.png')
With this, I'm getting everything I want, except for the presence of those margins. Here it is.
And what I want is something like this. I modified this image manually just to show you what I need. I need the image to not have those small margins. I am setting the x limits because I want the plot to reach the left and right borders of the image, but that black margin is getting in the way.
Using bbox_inches='tight'
and pad_inches=0
, seems to not work in newer versions of matplotlib, and using plt.savefig(fname, bbox_inches='tight', transparent=True, pad_inches=0)
didn't work for me either. I get the exact same result. Also tried extent = mpl.transforms.Bbox(((0, 0), (width, height)))
and then bbox_inches=extent
, but still nothing. None of those in that post.
How can I do this?
Thanks in advance.
EDIT 1
Added code to create the exact same array with which the plot was created.
Find the exact same audio here
EDIT 2
The rgb values for the ax.set_facecolor()
originally set it to black, but the images had gray facecolor. Fixed in this edit.