如何使用matplotlib显示两个数字?

时间:2011-10-12 18:25:29

标签: python matplotlib

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

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()

4 个答案:

答案 0 :(得分:71)

除了在脚本末尾调用plt.show()之外,您还可以分别控制每个数字:

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

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

raw_input()

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

注意:{3}在{3}中已将raw_input()重命名为input()

答案 1 :(得分:46)

您应该在创建所有图表后的最后调用plt.show()

答案 2 :(得分:8)

我有同样的问题。


做到了:

f1 = plt.figure(1)

# code for figure 1

#不要在这里写'plt.show()'


f2 = plt.figure(2)

# code for figure 2

plt.show()


在最后一个数字之后只写一次“ plt.show()”。 为我工作。

答案 3 :(得分:2)

或者,我建议在开始时和最后一个情节中打开互动,将其关闭。所有这些都会出现,但它们不会消失,因为你的程序会一直存在,直到你关闭数字。

import matplotlib.pyplot as plt
from matplotlib import interactive

plt.figure(1)
... code to make figure (1)

interactive(True)
plt.show()

plt.figure(2)
... code to make figure (2)

plt.show()

plt.figure(3)
... code to make figure (3)

interactive(False)
plt.show()