如何左对齐Python Matplotlib饼图?

我有一个左对齐标题的饼图:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
sizes = [15, 30, 45, 10]

fig1, ax1 = plt.subplots()
ax1.pie(sizes, autopct='%1.1f%%', startangle=90)
ax1.axis('equal')
ax1.set_title('Pie Title', loc='left')

plt.tight_layout()
plt.savefig(r'C:\images\pie.png', bbox_inches='tight')

此代码生成此图像:

《如何左对齐Python Matplotlib饼图?》

但饼图周围有太多的空白.如何左对齐标题和饼图并裁剪不必要的空白?

《如何左对齐Python Matplotlib饼图?》

最佳答案 您可以从方形图开始,例如: figsize =(4,4).

然后,您可以添加一些空格以使标题对齐.

import matplotlib.pyplot as plt

sizes = [15, 30, 45, 10]

fig1, ax1 = plt.subplots(figsize=(4,4))
ax1.pie(sizes, autopct='%1.1f%%', startangle=90)
ax1.axis('equal')
ax1.set_title('  Pie Title', loc='left')

plt.tight_layout()
plt.savefig(__file__+"pie.png", bbox_inches='tight')
plt.show()

《如何左对齐Python Matplotlib饼图?》

如果没有足够的空白间隙,你可以使用subplots_adjust,比如plt.subplots_adjust(bottom = 0,top = 0.93,left = 0.0,right = 1).

点赞