python – 为什么这段代码不能用标题保存我的数字?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 为什么这段代码不能用标题保存我的数字?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3235字,纯文字阅读大概需要5分钟。
内容图文
![python – 为什么这段代码不能用标题保存我的数字?](/upload/InfoBanner/zyjiaocheng/787/9d780df494854df5981be2096dd83106.jpg)
我用以下代码生成一些数字:
def boxplot_data(self,parameters_file,figure_title):
data = pandas.read_csv(parameters_file)
header = data.keys()
number_of_full_subplots = len(header)/16
remainder = len(header)-(16*number_of_full_subplots)
try:
for i in range(number_of_full_subplots+1):
fig =plt.figure(i)
txt = fig.suptitle(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots),fontsize='20')
txt.set_text(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots))
for j in range(16):
plt.ioff()
plt.subplot(4,4,j)
plt.boxplot(data[header[16*i+j]])
plt.xlabel('')
mng=plt.get_current_fig_manager()
mng.window.showMaximized()
plt.savefig(str(i)+'.png',bbox_inches='tight',orientation='landscape')
plt.close(fig)
plt.ion()
except IndexError:
txt = fig.suptitle(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots),fontsize='20')
txt.set_text(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots))
print '{} full figures were created and 1 partially filled figure containing {} subplots'.format(number_of_full_subplots,remainder)
这会以正确格式化的方式生成并保存数据,但无论我做什么,代码似乎都会绕过fig.suptitle行,因此我无法给出我的数字标题.抱歉,如果这个功能似乎有很多事情我没有解释,但有没有人解释为什么这个代码拒绝给我的数字标题?
解决方法:
你的问题并不是绕过了suptitle,而是你永远不会保存你称之为suptitle的数字.您对savefig的所有调用都在内部循环中,因此只保存子图.如果在代码运行时打开png文件,您实际上可以观察到这种情况 – 您会看到每个16个子轴都是逐个添加的.
您的代码看起来不必要复杂.例如,我认为你不需要使用ion和ioff.这是一个简单的例子,说明如何做我认为你想要的,然后翻译你的代码以适应它(显然我无法测试,因为我没有你的数据)
import matplotlib.pyplot as plt
test_y=range(10)
test_x=[8,13,59,8,81,2,5,6,2,3]
def subplotsave_test():
for i in range(5):
fig = plt.figure(i)
txt = fig.suptitle('Page '+str(i)+' of '+str(5),fontsize='20')
for j in range(16):
plt.subplot(4,4,j+1)
plt.plot(test_y,test_x)
plt.savefig(str(i)+'.png',bbox_inches='tight',orientation='landscape')
if __name__ == '__main__':
subplotsave_test()
我找到的一个提示对我有用 – 在你打算保存图形的地方做一个plt.show()并确保它看起来像你想要的那样然后用plt.savefig()替换那个调用
可能的功能翻译
def boxplot_data(self,parameters_file,figure_title):
data = pandas.read_csv(parameters_file)
header = data.keys()
number_of_full_subplots = len(header)/16
remainder = len(header)-(16*number_of_full_subplots)
for i in range(number_of_full_subplots+1)
fig =plt.figure(i)
fig.suptitle(figure_title+' (n='+str(len(data[header[0]]))+') '+'Page '+str(i)+' of '+str(number_of_full_subplots),fontsize='20')
for j in range(16):
plt.subplot(4,4,j+1)
if 16*i + j < len(header):
plt.boxplot(data[header[16*i+j]])
plt.xlabel('')
#You might want the showMaximized() call here - does nothing
#on my machine but YMMV
else:
print '{} full figures were created and 1 partially filled figure containing {} subplots'.format(number_of_full_subplots,remainder)
break
plt.savefig(str(i)+'.png',bbox_inches='tight',orientation='landscape')
plt.close(fig)
内容总结
以上是互联网集市为您收集整理的python – 为什么这段代码不能用标题保存我的数字?全部内容,希望文章能够帮你解决python – 为什么这段代码不能用标题保存我的数字?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。