0

In reference to this post: Removing white space around a saved image

I tried the suggestions on removing white space from my pie chart, but I still can't get rid of the space on the left and right, only top and bottom. When I change "ax.axis('equal')" to "off", it doesn't work, but leaves me with white space on all sides.

import matplotlib.pyplot as plt

labels = ['Fashion', 'Theatre Literature Arts', 'Manners and Customs', 'Image of Women', 'Love', 'Idea of Man', 'Politics', 'Reason', 'Family', 'Education and Formation', 'Structure of Society', 'Image of Men']
sizes = [31, 24, 16, 14, 12, 9, 3, 2, 2, 2, 2, 2]
colors = ['#33691E', '#9768D1', '#BDC6FF', '#FC7D49', '#CD0402', '#BEDB39', '#FFF176', '#FA5B0F', '#FE4D98', '#4A148C', '#8C8C8C', '#A5FFD2']


fig, ax = plt.subplots(constrained_layout=True)
ax.pie(sizes, autopct=None, colors=colors, shadow=False, startangle=90) 
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
for ax in fig.axes:
    ax.axis('equal')
    ax.margins(0,0)
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())
fig.tight_layout()
fig.savefig('.../Donna-galante1.png', transparent = True, pad_inches = 0, bbox_inches='tight', dpi=1200)

Does anyone know where the problem might be? I'm using jupyter notebook.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

2

Is this good enough? Basically, all I did is setting the figsize to something square and removing unnecessary commands. I'm not exactly sure why, but there is still a very thin margin on the left, right, and bottom side. It's frustrating, but I guess you can only really remove that by using a graphics tool, for example such as GIMP, or Inkscape (if you save it as an svg, which I would recommend anyways, because then you can add, remove, or alter whatever you like).

import matplotlib.pyplot as plt

labels = [
    'Fashion', 'Theatre Literature Arts', 'Manners and Customs',
    'Image of Women', 'Love', 'Idea of Man', 'Politics', 'Reason',
    'Family', 'Education and Formation', 'Structure of Society',
    'Image of Men'
]
sizes = [31, 24, 16, 14, 12, 9, 3, 2, 2, 2, 2, 2]
colors = [
    '#33691E', '#9768D1', '#BDC6FF', '#FC7D49', '#CD0402', '#BEDB39',
    '#FFF176', '#FA5B0F', '#FE4D98', '#4A148C', '#8C8C8C', '#A5FFD2'
]

fig, ax = plt.subplots(figsize=(4.8, 4.8))
ax.pie(sizes, autopct=None, colors=colors, shadow=False, startangle=90)
fig.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
ax.axis('equal')
ax.margins(0, 0)

fig.savefig(
    '.../Donna-galante1.png', transparent=True, pad_inches=0,
    bbox_inches='tight', dpi=1200
)

enter image description here

mapf
  • 1,906
  • 1
  • 14
  • 40