how to display plots before return to main menu in python

Viewed 78

It shows the barplot after i finished the loop. How can i show the barplot before it asks user for the input again?

1 Answers

You must ask sns to show the plot:

def menu():
    print('Please select one of the following options: \n')
    print('1. income per country')
    print('2. Marital status')
    print('3. Income and education level')
    print('4. food analysis')
    print('5. Exit')
    


    while True:
        try:
            option = int(input('Enter your option: '))

            if option ==1:
                eth = demo_data['country'].nunique()
                print('Number of country in the dataset: ', eth)
                print('Number of respondents per country: \n')
                demo_data['country'].value_counts()
                ax = sns.barplot(x = demo_data['Income'], y = demo_data['country'], data =  demo_data[demo_data['Age '] > 20]);
                ax.plot()
                plt.show()
                
                continue
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        if option <= 0:
            print("Sorry, your response must not be negative.")
            continue
        elif option >5:
            print("Sorry, your response must be within the list range.")
            continue
        else:
            break
menu()

which gives

enter image description here

Related