首页 > 解决方案 > 如何在 Matplotlib 中折叠饼图上方和轴标题下方的空间?

问题描述

出于某种原因,我在一对饼图和它们的标题之间得到了很大的空间。我不确定是什么控制了这个间距。

我想折叠这个空间。至少,我们可以把馅饼放在空间的中心吗?

这是一些类似于我实际在做的示例代码:

import matplotlib.pyplot as plt

x = ['a', 'b', 'c']
y = [1.1, 3.5, 2.2]
y1_sizes = y
y2_sizes = y

fig, axarr = plt.subplots(1, 2)
ax1 = axarr[0]
ax2 = axarr[1]

ax1.pie(y1_sizes, shadow=True, startangle=90, autopct='%1.1f%%')
ax1.axis('equal')
ax1.set_title('Old Stuff')

ax2.pie(y2_sizes, shadow=True, startangle=90, autopct='%1.1f%%')
ax2.axis('equal') 
ax2.set_title('New Stuff')

lgd1 = ax1.legend(x, loc='lower center')
lgd2 = ax2.legend(x, loc='lower center')

fig.suptitle('My Chart Title', fontweight='bold')

这是结果

在此处输入图像描述

标签: pythonmatplotlib

解决方案


我在评论中回答你的问题:“有没有办法向下移动轴标题?”。您可以通过使用参数在分数坐标中指定各个标题的垂直位置来实现此目的y

ax1.set_title('Old Stuff', y=0.8)
ax2.set_title('New Stuff', y=0.8)
fig.suptitle('My Chart Title', fontweight='bold', y=0.85)

在此处输入图像描述


推荐阅读