如何使用matplotlib显示两个数字? [英] How to show two figures using matplotlib?

查看:73
本文介绍了如何使用matplotlib显示两个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同时绘制两个图形时遇到了一些麻烦,没有在一个图中显示.但是根据文档,我编写了代码,只有图1所示.我想也许我失去了一些重要的东西.有人可以帮我弄清楚吗?谢谢. (代码中使用的* tlist_first *是数据列表.)

I have some troubles while drawing two figures at the same time, not shown in a single plot. But according to the documentation, I wrote the code and only the figure one shows. I think maybe I lost something important. Could anyone help me to figure out? Thanks. (The *tlist_first* used in the code is a list of data.)

plt.figure(1)
plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')

plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend()
plt.xlim(0,120)
plt.ylim(0,1) 
plt.show()
plt.close() ### not working either with this line or without it

plt.figure(2)
plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')

plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')

plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend(loc= 4)
plt.xlim(0,2640)
plt.ylim(0,1)
plt.show()

推荐答案

或者在脚本末尾调用plt.show(),也可以分别控制每个图形:

Alternatively to calling plt.show() at the end of the script, you can also control each figure separately doing:

f = plt.figure(1)
plt.hist........
............
f.show()

g = plt.figure(2)
plt.hist(........
................
g.show()

raw_input()

在这种情况下,您必须致电raw_input来保持数字的生命. 这样,您可以动态选择要显示的数字

In this case you must call raw_input to keep the figures alive. This way you can select dynamically which figures you want to show

注意:raw_input()在Python 3中已重命名为input()

Note: raw_input() was renamed to input() in Python 3

这篇关于如何使用matplotlib显示两个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆