2024-04-24 22:08:45 发布
网友
例如,当我想保存一个文件并使用
import matplotlib.pyplot as plt fig=plt.figure(1) plt.plot([1,2,3,4]) t=0.1 fig.savefig("filename%f.png" % t)
文件名为“filename0.100000.png” 如何控制位数,并决定将文件保存为“filename0.10.png”?你知道吗
把t当作一个字符串,会更简单。你知道吗
fig.savefig("filename{}.png".format(getID(9))
对上述代码最简单的更改是在%和f之间添加精度说明符,例如%.2f:
%
f
%.2f
In [1]: "filename%.2f.png" % 0.1 Out[1]: 'filename0.10.png'
这种格式说明符在library documentation for the string types中进行了描述,但正如该节顶部的注释所提到的,值得研究更新和更灵活的选项,如f-strings和^{} method。你知道吗
把t当作一个字符串,会更简单。你知道吗
对上述代码最简单的更改是在
%
和f
之间添加精度说明符,例如%.2f
:这种格式说明符在library documentation for the string types中进行了描述,但正如该节顶部的注释所提到的,值得研究更新和更灵活的选项,如f-strings和^{} method 。你知道吗
相关问题 更多 >
编程相关推荐