如何保存整个图形而不将其切断?

时间:2019-05-01 21:04:23

标签: python matplotlib png data-visualization pie-chart

我正在尝试使用myplotlib绘制饼图的图形,并且我能够设置间距和使用plt.show()的最终结果正是我想要的。但是,每当我尝试使用plt.savefig("file path name here.png")保存图形时,标题和标签的部分都会被切除。

我尝试使用plt.tight_layout(),但这完全消除了我之前设置的所有间距。我还尝试了plt.savefig("file path name.png", dpi=...),但是这只是增加了保存的png的大小,因此创建了一个更大的图像,该图像的标题和标签被切除了。

Dict_July=dict(Apps["July"].value_counts())
Dict_Aug=dict(Apps["August"].value_counts())
Dict_Sept=dict(Apps["September"].value_counts())
Dict_Oct=dict(Apps["October"].value_counts())
Dict_Nov=dict(Apps["November"].value_counts())
Dict_Jan=dict(Apps["January"].value_counts())
Dict_Feb=dict(Apps["Feb/Mar"].value_counts())
Dict_April=dict(Apps["April"].value_counts())

App_Errors=[Dict_July, Dict_Aug, Dict_Sept, Dict_Oct, Dict_Nov, Dict_Jan, Dict_Feb, Dict_April]

App_Names=["None","2020","CRM","Other","3C Logic","Outlook","Act-on","Foxit"]
colors=["c","orangered","lime","gold","mediumorchid","mediumslateblue","fuchsia","mediumspringgreen"]
july_leg_values=[]
july_legend=[]

for x in range(len(App_Names)):
    try:
        july_leg_values.append(App_Errors[0][App_Names[x]])
        july_legend.append(App_Names[x])
    except:
        "test"

plt.pie(july_leg_values, labels=july_legend,shadow=True,colors=colors, autopct="%1.1f%%", radius=1.6, explode=(.1,0,0,0,0,0,0,0))
plt.title("July 2017 App Errors",y=1.3,fontsize=14, fontweight="bold")

plt.savefig("./Images/July_App.png")

here是我创建的内容和另存为的示例。

1 个答案:

答案 0 :(得分:1)

使用bbox_inches='tight'

plt.savefig('file_name.png', bbox_inches='tight')