【问题标题】:How to show two figures using matplotlib?如何使用 matplotlib 显示两个数字?
【发布时间】:2011-12-06 09:06:03
【问题描述】:

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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我遇到了同样的问题。


    做到了:

    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()'。 为我工作。

    【讨论】:

    • 这个和from janneb back in 2011的答案一样
    • 这显示在同一个窗口中,而不是 2 个单独的窗口中。虽然回答了OP的问题。因此投票赞成。
    • 但是如果你想要一个单独的情节怎么办?它被绘制在同一个地块上
    【解决方案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()
    

    【讨论】:

      【解决方案3】:

      除了在脚本末尾调用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()

      【讨论】:

      • 不幸的是,使用 python3.6 和最新的 matplotlib,调用几个 fig.show() 似乎什么也没显示。最后我还是要调用 plt.show() 。
      • @kakyo - 使用 Python 3.6.6 和 Matplotlib 2.2.2(这是您撰写本文时的最新版本);上面的解决方案对我有用。您的问题必须来自其他方面,例如使用的backend。运行matplotlib.get_backend(),我得到'Qt5Agg'
      • 我还必须将figure=g 添加到第二个plt.hist()
      • 我需要其他软件包吗? NameError: name 'raw_input' is not defined
      • @zheyuanWang 如果你使用python 3,你必须使用input()。请参阅帖子中的最后一条说明
      【解决方案4】:

      您应该只在创建所有地块后最后调用plt.show()

      【讨论】:

      • 我觉得这很烦人,因为如果我打电话给show()一次,我就不能再打电话了,如果我想再次显示情节,我必须重新绘制一遍?
      • 我认为每次调用时都有显示情节的功能是合理的。
      猜你喜欢
      • 2021-05-28
      • 2019-08-21
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多