为什么我的 python 应用程序在显示图表后立即退出?

Why does my python app exits immediately after show graph diagram?

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

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)

如果您在这些情况下使用 raw_input / 在程序结束时输入 window 将等待击键

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

试试这个:

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

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

var = raw_input("Press something to quit")

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