-2

I use below code to show a data graph but the graph window is just shown for one second then the application exits. Below is my code. Is there anything wrong with it?

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)
Mathias711
  • 6,568
  • 4
  • 41
  • 58
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • As soon as the execution is finished the program would exit because there is no more execution commands.You can ask for an input from user to avoid the exit. – Sandeep Hukku Apr 11 '17 at 11:09
  • 4
    Possible duplicate of [Keep plotting window open in Matplotlib](http://stackoverflow.com/questions/12358312/keep-plotting-window-open-in-matplotlib) – SiHa Apr 11 '17 at 11:15

3 Answers3

0

if you use raw_input / input in these case at end of program window will wait for key stroke

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

Try this:

  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')
  • 1
    I got `pause: command not found` error. Does this relate to platform? I am using MacOS. – Joey Yi Zhao Apr 11 '17 at 23:45
  • Yeah , or instead of os.system('pause') use input("Press any key to close") if you using python3 else use raw_input("Press any key to close") , this will prompt a message after program end and hit of any keystroke will close your program. – devotedCoder Apr 12 '17 at 05:10
0

In case you want a user to be informed that action is required in order to break the program execution you can do it like this:

var = raw_input("Press something to quit")

The above (first) answer mentioning usage of system('pause') doesn't work with my OSX

trust512
  • 2,188
  • 1
  • 18
  • 18