是否有使用 matplotlib 指定饼图中标题的位置?

tas*_*eel 2 python matplotlib pie-chart

我不知道如何使用 matplotlib 指定标题的位置。我希望标题离标签远一点,但我不知道如何设置。

labels = df03['new_sentiment']
count = df03['count']

fig1, ax1 = plt.subplots()
ax1.pie(count, labels=labels, autopct='%1.1f%%')
ax1.axis('equal')
plt.title('7D-prior-3MD_sem_sen')
plt.savefig('04_7D-prior-3MD_sem_sen.tiff', dpi=300, format='tiff', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

aur*_*rel 5

您可以简单地向 plt.title() 添加一个 'pad' 参数:

import matplotlib.pyplot as plt
count = [3, 4, 5]
labels = ['Neutral', 'Negative', 'Positive']
fig1, ax1 = plt.subplots()
ax1.pie(count, labels=labels, autopct='%1.1f%%')
ax1.axis('equal')
plt.title('7D-prior-3MD_sem_sen', pad=32)
plt.show()
Run Code Online (Sandbox Code Playgroud)

增加或减少该值以偏移标题的 y 坐标。