如何使用matplotlib显示两个图形?

2024-05-19 01:40:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在同时画两个图形时遇到了一些麻烦,没有在一个图中显示出来。但是根据文档,我编写了代码,只有图1显示。我想也许我失去了一些重要的东西。有谁能帮我弄清楚吗?谢谢。(代码中使用的*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()

Tags: 代码trueplthistlabelcolorfirstfigure
3条回答

我也有同样的问题。


是否:

f1 = plt.figure(1)

# code for figure 1

# don't write 'plt.show()' here


f2 = plt.figure(2)

# code for figure 2

plt.show()


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

除了在脚本末尾调用plt.show()之外,还可以分别控制每个图形执行以下操作:

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

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

raw_input()

在这种情况下,您必须调用raw_input来保持图形的活动性。 这样可以动态选择要显示的图形

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

只有在创建所有绘图之后,才应该在末尾调用plt.show()

相关问题 更多 >

    热门问题