【问题标题】:Why does my python app exits immediately after show graph diagram?为什么我的 python 应用程序在显示图表后立即退出?
【发布时间】:2017-04-11 10:52:25
【问题描述】:

我使用下面的代码来显示数据图表,但图表窗口仅显示一秒钟,然后应用程序退出。下面是我的代码。有什么问题吗?

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

train_df = pd.read_csv('./data/train.csv')

g = sns.FacetGrid(train_df, col='Survived')
g.map(plt.hist, 'Age', bins=20)

【问题讨论】:

标签: python matplotlib seaborn


【解决方案1】:

试试这个:

  import os    
  import seaborn as sns
  import pandas as pd
  import matplotlib.pyplot as plt

  train_df = pd.read_csv('./data/train.csv')

  g = sns.FacetGrid(train_df, col='Survived')
  g.map(plt.hist, 'Age', bins=20)
  os.system('pause')

【讨论】:

  • 我收到了pause: command not found 错误。这与平台有关吗?我正在使用 MacOS。
  • 是的,或者代替 os.system('pause') 使用 input("Press any key to close") 如果你使用 python3 否则使用 raw_input("Press any key to close") ,这个程序结束后将提示一条消息,任何击键都会关闭您的程序。
【解决方案2】:

如果您在这些情况下使用 raw_input / input 在程序窗口结束时将等待击键

    import seaborn as sns
    import pandas as pd
    import matplotlib.pyplot as plt

    train_df = pd.read_csv('./data/train.csv')

    g = sns.FacetGrid(train_df, col='Survived')
    g.map(plt.hist, 'Age', bins=20)

    #use this if you are using python2
    raw_input("Press any key to close")
    #use this if you are using python 3
    input("Press any key to close")

【讨论】:

    【解决方案3】:

    如果您希望通知用户需要采取措施来中断程序执行,您可以这样做:

    var = raw_input("Press something to quit")
    

    上面提到 system('pause') 用法的(第一个)答案不适用于我的 OSX

    【讨论】:

    • raw_input 不起作用,因为它让 GUI 窗口变得无响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多