简体   繁体   English

将字典的键转换为字符串

[英]Convert the key of a dictionary into a string

I think that the problem of my code is that each time the loop in iterating on a new variable, the previous variable is not deleted.我认为我的代码的问题是每次循环迭代一个新变量时,前一个变量没有被删除。 So I end up with plot that contains all the previous data coming from the previous iterations.所以我最终得到了包含来自先前迭代的所有先前数据的图。 What I may need is a function to clear up the memory at the end of each loop... The reason while I originally asked this question, was that I run the same loop over and over and I end up having to many data on my plot (since it seems like the data are not clear at the end of each iteration).我可能需要的是在每个循环结束时清除内存的函数......我最初问这个问题的原因是我一遍又一遍地运行相同的循环,最终我的数据上有很多数据绘图(因为似乎每次迭代结束时数据都不清楚)。

dicohist = {
'couts-formels': dftotal.total_cout_formels,
'couts_direct': dftotal.cout_total,
'perte-de-prod': dftotal.snt_prix
}

for key in dicohist:
    histo = Series.hist(dicohist[key],bins=50)  
    histo.set_xlabel("cout en euros")
    histo.set_ylabel("nombre de sujets")
    histo.set_title(key)
    fig = histo.get_figure()
    fig.savefig('/Users/Salim/Desktop/plot/%s.png'%(key))   

我在循环结束时添加了一个 pyplot 函数:plt.close() 解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM