Ask coding questions

← Back to all posts
not sure why I cannot visualize the bars graphic in the idle
CarolinaValcarc (0)

Not sure, why I cannot visualize the bar graphic when I work with the idle of pyton. I have not installed jupiter or sublime. I just using the idle of python because I feel comfortable. But I can not see the graphics. I see properly the dataframes and near all, but not any answer when I call seaborn and catplot(). Just the answer in console is:
<seaborn.axisgrid.FacetGrid object at 0x033F0520>

All my code is:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib
matplotlib.use ('tkAgg')
import matplotlib.pyplot as plt

Import data

df = pd.read_csv('matplot graphics/medical_examination.csv')

Add 'overweight' column

BMI=df['weight']/((df['height'])/100)**2
df['overweight'] = ['0' if bmi<=25 else '1' for bmi in BMI]
print(df['overweight'])

Normalize data by making 0 always good and 1 always bad. If the value of 'cholestorol' or 'gluc' is 1, make the value 0.

#If the value is more than 1, make the value 1.
df['cardio']= np.where((df['cholesterol']<=1) | (df['gluc']<=1), '0', '1' )
print(df)

Draw Categorical Plot

#def draw_cat_plot():

# Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.

df_cat= pd.melt(df, id_vars=['cardio'], value_vars=['cholesterol','gluc','smoke','alco','active','overweight'])

Group and reformat the data to split it by 'cardio'. Show the counts of each feature.

df_cat=df_cat.groupby(['cardio', 'variable', 'value'])['value'].count().reset_index(name='total')
print(df_cat)

Draw the catplot with 'sns.catplot()'

graph = sns.catplot(x='variable',y='total', col='cardio',data=df_cat, kind='bar', hue='value')
graph.set(xlabel='variable',ylabel='total')
graph.set_xticklabels(['active','alco','cholesterol','gluc','overweight', 'smoke',])

fig=graph.fig
print(graph)
plt.figure().show()
ax=fig.axes[0].legend()

Comments
hotnewtop
CarolinaValcarc (0)

Anyway, already solved. And project finished. Good day