Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -123,16 +123,24 @@ def generar_mapa_calor(df):
|
|
| 123 |
# Gr谩fico univariado
|
| 124 |
def grafico_univariado(var, genero, localidades, compromiso_renal, antecedentes):
|
| 125 |
df = aplicar_filtros(data, genero, localidades, compromiso_renal, antecedentes)
|
| 126 |
-
if df.empty:
|
| 127 |
return None
|
|
|
|
| 128 |
plt.figure(figsize=(6, 4))
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
else:
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
plt.title(f"Distribuci贸n de {var}")
|
| 134 |
-
path = f"uni_{var}.png"
|
| 135 |
plt.tight_layout()
|
|
|
|
| 136 |
plt.savefig(path)
|
| 137 |
plt.close()
|
| 138 |
return path
|
|
@@ -142,19 +150,24 @@ def grafico_bivariado(x, y, genero, localidades, compromiso_renal, antecedentes)
|
|
| 142 |
df = aplicar_filtros(data, genero, localidades, compromiso_renal, antecedentes)
|
| 143 |
if df.empty or x not in df.columns or y not in df.columns:
|
| 144 |
return None
|
|
|
|
| 145 |
plt.figure(figsize=(6, 4))
|
|
|
|
| 146 |
if pd.api.types.is_numeric_dtype(df[x]) and pd.api.types.is_numeric_dtype(df[y]):
|
| 147 |
-
sns.scatterplot(data=df, x=x, y=y)
|
| 148 |
elif pd.api.types.is_numeric_dtype(df[x]) and not pd.api.types.is_numeric_dtype(df[y]):
|
| 149 |
-
sns.boxplot(data=df, x=y, y=x)
|
| 150 |
plt.xticks(rotation=30)
|
| 151 |
elif pd.api.types.is_numeric_dtype(df[y]) and not pd.api.types.is_numeric_dtype(df[x]):
|
| 152 |
-
sns.boxplot(data=df, x=x, y=y)
|
| 153 |
plt.xticks(rotation=30)
|
| 154 |
else:
|
| 155 |
orden_x = df[x].value_counts().index
|
| 156 |
-
sns.countplot(data=df, x=x, hue=y, order=orden_x)
|
|
|
|
|
|
|
| 157 |
plt.xticks(rotation=30)
|
|
|
|
| 158 |
plt.title(f"{x} vs {y}")
|
| 159 |
plt.tight_layout()
|
| 160 |
path = f"bi_{x}_{y}.png"
|
|
|
|
| 123 |
# Gr谩fico univariado
|
| 124 |
def grafico_univariado(var, genero, localidades, compromiso_renal, antecedentes):
|
| 125 |
df = aplicar_filtros(data, genero, localidades, compromiso_renal, antecedentes)
|
| 126 |
+
if df.empty or var not in df.columns:
|
| 127 |
return None
|
| 128 |
+
|
| 129 |
plt.figure(figsize=(6, 4))
|
| 130 |
+
ax = None
|
| 131 |
+
if pd.api.types.is_numeric_dtype(df[var]):
|
| 132 |
+
ax = sns.histplot(df[var].dropna(), kde=True)
|
| 133 |
else:
|
| 134 |
+
orden = df[var].value_counts().index
|
| 135 |
+
ax = sns.countplot(data=df, x=var, order=orden)
|
| 136 |
+
for container in ax.containers:
|
| 137 |
+
ax.bar_label(container, fontsize=9, label_type='edge')
|
| 138 |
+
|
| 139 |
+
plt.xticks(rotation=30)
|
| 140 |
+
|
| 141 |
plt.title(f"Distribuci贸n de {var}")
|
|
|
|
| 142 |
plt.tight_layout()
|
| 143 |
+
path = f"uni_{var}.png"
|
| 144 |
plt.savefig(path)
|
| 145 |
plt.close()
|
| 146 |
return path
|
|
|
|
| 150 |
df = aplicar_filtros(data, genero, localidades, compromiso_renal, antecedentes)
|
| 151 |
if df.empty or x not in df.columns or y not in df.columns:
|
| 152 |
return None
|
| 153 |
+
|
| 154 |
plt.figure(figsize=(6, 4))
|
| 155 |
+
ax = None
|
| 156 |
if pd.api.types.is_numeric_dtype(df[x]) and pd.api.types.is_numeric_dtype(df[y]):
|
| 157 |
+
ax = sns.scatterplot(data=df, x=x, y=y)
|
| 158 |
elif pd.api.types.is_numeric_dtype(df[x]) and not pd.api.types.is_numeric_dtype(df[y]):
|
| 159 |
+
ax = sns.boxplot(data=df, x=y, y=x)
|
| 160 |
plt.xticks(rotation=30)
|
| 161 |
elif pd.api.types.is_numeric_dtype(df[y]) and not pd.api.types.is_numeric_dtype(df[x]):
|
| 162 |
+
ax = sns.boxplot(data=df, x=x, y=y)
|
| 163 |
plt.xticks(rotation=30)
|
| 164 |
else:
|
| 165 |
orden_x = df[x].value_counts().index
|
| 166 |
+
ax = sns.countplot(data=df, x=x, hue=y, order=orden_x)
|
| 167 |
+
for container in ax.containers:
|
| 168 |
+
ax.bar_label(container, fontsize=8, label_type='edge')
|
| 169 |
plt.xticks(rotation=30)
|
| 170 |
+
|
| 171 |
plt.title(f"{x} vs {y}")
|
| 172 |
plt.tight_layout()
|
| 173 |
path = f"bi_{x}_{y}.png"
|