使用matplotlib绘制饼图时如何调整标题位置使其远离标签
解决Matplotlib饼图标题距离饼图标签过近的问题
你可以通过plt.title()的两个参数灵活控制标题位置,实现拉开标题与饼图标签间距的需求:
y参数:控制标题的垂直位置,默认值为1.0(与子图顶部对齐),数值越大标题越靠上,你可以根据实际显示效果调整到1.1~1.2区间即可pad参数:直接设置标题与子图顶部的内边距,单位为磅,数值越大间距越大
修改后的代码示例
labels = df03['new_sentiment'] count = df03['count'] fig1, ax1 = plt.subplots() ax1.pie(count, labels=labels, autopct='%1.1f%%') ax1.axis('equal') # 调整标题位置,y和pad参数可根据实际显示效果微调 plt.title('7D-prior-3MD_sem_sen', y=1.15, pad=20) plt.savefig('04_7D-prior-3MD_sem_sen.tiff', dpi=300, format='tiff', bbox_inches='tight')
如果你偏好面向对象的写法,也可以将标题设置语句替换为:
ax1.set_title('7D-prior-3MD_sem_sen', y=1.15, pad=20)
原绘制效果参考

内容的提问来源于stack exchange,提问作者tassaneel
相关产品推荐
相关产品推荐

