【问题标题】:Removing white space around pie chart in matplotlib删除matplotlib中饼图周围的空白
【发布时间】:2020-05-06 14:44:57
【问题描述】:

参考这篇帖子:Removing white space around a saved image in matplotlib

我尝试了从我的饼图中删除空白的建议,但我仍然无法摆脱左侧和右侧的空间,只有顶部和底部。当我将“ax.axis('equal')”更改为“off”时,它不起作用,但给我留下了四面八方的空白。

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)

有人知道问题出在哪里吗? 我正在使用 jupyter 笔记本。

提前感谢您!

【问题讨论】:

  • 您好,请提供MWE,以便我们为您提供帮助。
  • 嗨@mapf! :) 我编辑了帖子,希望现在没问题。

标签: python matplotlib charts pie-chart


【解决方案1】:

这够好吗?基本上,我所做的只是将figsize 设置为正方形并删除不必要的命令。左侧、右侧和底部仍然留有很薄的边距,我不完全确定为什么,这令人沮丧,但我想你只能通过使用图形工具来真正删除它,例如 GIMP .

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
)

【讨论】: