0

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.

enter image description here

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.

enter image description here

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.

TheSprinter
  • 338
  • 1
  • 12
  • `plt.savefig('samples.png', bbox_inches='tight', pad_inches=0)`An image with no correct margins has been saved. My environment is 'matplotlib:3.3.0'. – r-beginners Jul 23 '20 at 08:40
  • @r-beginners I don't really understand. Is that an error you got? I didn't get an error with that, just the same image with the small margins. – TheSprinter Jul 23 '20 at 08:58
  • @TheSprinter can you post reproducible example that will create similar image? – Zaraki Kenpachi Jul 23 '20 at 09:02
  • No errors occurred and the image was saved with no margins. – r-beginners Jul 23 '20 at 09:02
  • @ZarakiKenpachi Thanks for your comment. The code serves perfectly as reproducible example to see the margins. However, see edit 1 if you want to plot the exact same image. – TheSprinter Jul 23 '20 at 09:17
  • @r-beginners You sure? Did you look closely? I downloaded from https://anaconda.org/conda-forge/matplotlib, which is the same version. – TheSprinter Jul 23 '20 at 09:18
  • @ZarakiKenpachi Well, yes, I realized the `samples` array wasn't created in the orignal post. – TheSprinter Jul 23 '20 at 09:52
  • Using the updated sample data, the images created and saved as graphs do indeed show extra margins at both ends. Fixing it to `dpi=384` removes the margins. This setting is manual: `dpi=500 may have been too big. – r-beginners Jul 23 '20 at 10:04
  • @r-beginners Tried with `dpi=384` but still got the same margin. Two questions: Where did that number (384) come from? How does the resolution of the image affect whether it has margins or not? – TheSprinter Jul 23 '20 at 10:57
  • I just matched it to the screen resolution, which is 96 DPI, so I just tripled it. This doesn't make a lot of sense, you might want to try adjusting the DPI in various ways. – r-beginners Jul 23 '20 at 11:59
  • @r-beginners Yeah, it doesn't. I might get it to work in my machine, but I need it to work anywhere, with any screen resolution; so what I really need is some function or method that will effectively remove that margin. – TheSprinter Jul 23 '20 at 12:35
  • I think I gave you some extra information. I was able to achieve this by manually changing the DPI value. So you can try that too and if the issue doesn't resolve itself, it's another issue. – r-beginners Jul 23 '20 at 12:58

1 Answers1

0

I got help somewhere else and these borders/margins are called spines.

They are deleted in the following way, in my case:

ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
TheSprinter
  • 338
  • 1
  • 12