我想绘制一个饼图,该饼图显示数据集的比例,并保存该图形以供以后检查。我使用jupyterlab。
问题是plt.show()和plt.savefig()的图像不同,尤其是阴影。
“ show”的图片:
https://drive.google.com/open?id=1hWMIRtCvtp6s5TR7b25pTm5kQDJ5tLMG
savefig的图片:
https://drive.google.com/open?id=1Z0fgfJeWpWoARUMIjtNxvp3yIEQjvJVW
我该如何解决这个问题?
# Plot pie chart
plt.rcParams['savefig.dpi']=300
data_for_plot=pd.Series([316, 51])
legend_label=['Not UpperGI', 'UpperGI']
explode=(0, 0.1)
fig, ax = plt.subplots(figsize=(10,8), subplot_kw=dict(aspect="equal"))
def func(pct, allvals):
absolute = int(np.round(pct/100.*np.sum(allvals), 0))
return "{:.1f}%\n({:d})".format(pct, absolute)
wedges, texts, autotexts=ax.pie(data_for_plot, explode=explode, labels=legend_label, autopct=lambda pct:func(pct,data_for_plot), shadow=True, startangle=90)
legend=ax.legend(wedges, legend_label, title="UpperGI", loc='upper right', fontsize=18, bbox_to_anchor=(1.25,0.75))
plt.setp(texts, size=20, weight='bold', color='k')
plt.setp(autotexts, size=20, weight='bold', color='w')
plt.setp(legend.get_title(), fontsize=20)
ax.set_title("Dataset_Proportion", fontsize=20)
fig.savefig('./temp.png', bbox_inches='tight')
plt.show()
我希望图像相同。 预先谢谢你。
---编辑---
设置bbox_inches =“紧”不变。