1

I am trying to plot a white ellipse on a coloured (in my case gray) background. Nothing else, so no axis, no white space and no frame. I managed to remove the axis, but there still is a white border around the image.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse

ell = Ellipse(xy=np.random.randint(3,7,2),
    width=np.random.randint(2,3), height=np.random.randint(4,6),
    angle=np.random.rand() * 360,
    color='white', fill=True)

fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})

ell.set_facecolor('white')

ax.add_artist(ell)
ax.set_facecolor((0.5, 0.5, 0.5))
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)

fig.savefig("test.png",bbox_inches='tight')

plt.show()

plt.show

Removing the frame with

fig = plt.figure(frameon=False)

before saving it didn't work either, because it basically removes everything, resulting in an empty image.

test.png

I also tried

ax.axis('off')

but that removed the coloured (gray) background.

Any help is highly appreciated, thank you very much.

M. Evans
  • 11
  • 3
  • i don't see white border anywhere, only black? – dejanmarich Nov 08 '18 at 12:17
  • Check the duplicates above. In short, you could add `fig.subplots_adjust(left=0, right=1, bottom=0, top=1)` to remove the whitespace around the Axes – tmdavison Nov 08 '18 at 12:18
  • @DejanMarić Maybe it isn't called border, sorry. I mean the white space around the image. I've marked it here in red. (And I've also marked the black area that I want to remove aswell). https://i.imgur.com/Q9SN0pB.png – M. Evans Nov 09 '18 at 00:21
  • @tmdavison I added the line you recommended and tried the solutions from the links you provided, but it didn't work. Which got me thinking; I wrote 'ax.axes.get_xaxis().set_visible(False)', doesn't that mean that the axis is still there but just invisible? So, does that mean, I can not remove the whitespace around the image because it's not just whitespace but an invisible axis? What I want in the end is to remove the marked areas in this [picture](https://i.imgur.com/Q9SN0pB.png) and to get something like [that](https://i.imgur.com/n51uRiD.png) (cut out with paint). – M. Evans Nov 09 '18 at 00:37
  • you also need to get rid of `bbox_inches='tight'`, and make the figure square. – tmdavison Nov 09 '18 at 11:20
  • @tmdavison Removing `bbox_inches='tight'` removed the white space at the bottom, but now the white space on the left and right hand side got wider [link](https://i.imgur.com/jFP9YI9.png). And what do you mean by making the figure square? Could you give me another hint? Thank you very much. – M. Evans Nov 10 '18 at 01:31
  • put `figsize=(5, 5)` inside the `plt.subplots()` call. That will make the figure 5x5 inches. – tmdavison Nov 10 '18 at 17:53
  • Thanks. I also set `fig.subplots_adjust(left=-0.01, right=1.1, bottom=0, top=1.1)` and got the image I wanted. – M. Evans Nov 11 '18 at 23:49

0 Answers0