Update app.py
Browse files
app.py
CHANGED
|
@@ -111,117 +111,87 @@ else:
|
|
| 111 |
# Procesar valores
|
| 112 |
y_values_lists = [[float(i) for i in y_values.split(",") if i.strip()] for y_values in y_values_list]
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
fig.update_traces(
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
fig.update_traces(
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
fig.
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
trace.marker.color = colors[i % len(colors)]
|
| 179 |
-
trace.marker.line.color = border_colors[i % len(border_colors)]
|
| 180 |
-
trace.marker.line.width = border_width
|
| 181 |
-
else:
|
| 182 |
-
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 183 |
-
fig.update_layout(bargap=0.2)
|
| 184 |
-
elif chart_type == "Donut":
|
| 185 |
-
figs = []
|
| 186 |
-
for i, y_name in enumerate(y_names_list):
|
| 187 |
-
fig = px.pie(data, values=y_name, names="X", hole=hole_size, color_discrete_sequence=colors[:len(x)])
|
| 188 |
-
fig.update_traces(hovertemplate='<b>%{label}</b>: %{value} (%{percent})', textinfo='percent+label')
|
| 189 |
-
fig.update_layout(
|
| 190 |
-
title=dict(
|
| 191 |
-
text=f"{chart_title}<br><sub>{y_name}</sub>" if len(y_names_list) > 1 else chart_title,
|
| 192 |
-
x=0.5,
|
| 193 |
-
y=0.95,
|
| 194 |
-
xanchor='center',
|
| 195 |
-
yanchor='top',
|
| 196 |
-
font=dict(
|
| 197 |
-
family=font_family,
|
| 198 |
-
size=18,
|
| 199 |
-
color="#374151",
|
| 200 |
-
weight="normal"
|
| 201 |
-
)
|
| 202 |
-
),
|
| 203 |
-
**common_layout
|
| 204 |
-
)
|
| 205 |
-
figs.append(fig)
|
| 206 |
-
for fig in figs:
|
| 207 |
-
st.plotly_chart(fig)
|
| 208 |
-
elif chart_type == "Línea temporal":
|
| 209 |
-
if horizontal_bars:
|
| 210 |
-
fig = px.bar(data, x="Y", y="X", orientation='h', barmode='stack')
|
| 211 |
-
else:
|
| 212 |
-
fig = px.bar(data, x="X", y="Y", barmode='stack')
|
| 213 |
-
fig.update_traces(hovertemplate=hovertemplate)
|
| 214 |
-
if use_multiple_colors:
|
| 215 |
fig.update_traces(marker_color=colors[:len(x)], marker_line_color=border_colors[:len(x)], marker_line_width=border_width)
|
| 216 |
else:
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
fig.update_layout(
|
| 223 |
title=dict(
|
| 224 |
-
text=f"{chart_title}",
|
| 225 |
x=0.5,
|
| 226 |
y=0.95,
|
| 227 |
xanchor='center',
|
|
@@ -232,22 +202,44 @@ else:
|
|
| 232 |
color="#374151",
|
| 233 |
weight="normal"
|
| 234 |
)
|
| 235 |
-
)
|
|
|
|
| 236 |
)
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
fig.update_layout(**common_layout)
|
| 240 |
-
|
| 241 |
-
# Aplicar múltiples colores si se seleccionó la opción
|
| 242 |
-
if use_multiple_colors and chart_type != "Donut":
|
| 243 |
-
for i, trace in enumerate(fig.data):
|
| 244 |
-
trace.update(marker_color=colors[i % len(colors)], marker_line_color=border_colors[i % len(border_colors)], marker_line_width=border_width)
|
| 245 |
-
|
| 246 |
-
# Mostrar el gráfico
|
| 247 |
st.plotly_chart(fig)
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# Información adicional
|
| 253 |
st.write("Aquí puede añadir información adicional sobre los gráficos generados.")
|
|
|
|
| 111 |
# Procesar valores
|
| 112 |
y_values_lists = [[float(i) for i in y_values.split(",") if i.strip()] for y_values in y_values_list]
|
| 113 |
|
| 114 |
+
# Asegurar que todas las listas de Y tengan el mismo tamaño que X
|
| 115 |
+
for y_values in y_values_lists:
|
| 116 |
+
while len(y_values) < len(x):
|
| 117 |
+
y_values.append(0)
|
| 118 |
+
while len(y_values) > len(x):
|
| 119 |
+
y_values.pop()
|
| 120 |
+
|
| 121 |
+
# Crear un DataFrame
|
| 122 |
+
data = pd.DataFrame({"X": x})
|
| 123 |
+
for idx, y_set in enumerate(y_values_lists):
|
| 124 |
+
data[y_names_list[idx]] = y_set
|
| 125 |
+
|
| 126 |
+
# Configuración común para todos los gráficos
|
| 127 |
+
common_layout = dict(
|
| 128 |
+
xaxis_title=x_label,
|
| 129 |
+
yaxis_title=y_label,
|
| 130 |
+
plot_bgcolor="white",
|
| 131 |
+
hovermode="x unified",
|
| 132 |
+
width=graph_width,
|
| 133 |
+
height=graph_height,
|
| 134 |
+
margin=dict(l=60, r=40, t=100, b=40),
|
| 135 |
+
xaxis=dict(showgrid=True, zeroline=True, gridcolor='rgba(211,211,211,0.5)', zerolinecolor='rgba(128,128,128,0.5)', autorange=True),
|
| 136 |
+
yaxis=dict(showgrid=True, zeroline=True, gridcolor='rgba(211,211,211,0.5)', zerolinecolor='rgba(128,128,128,0.5)', autorange=True),
|
| 137 |
+
font=dict(family=font_family, size=18, color="black"),
|
| 138 |
+
showlegend=show_legend,
|
| 139 |
+
legend=dict(orientation="v", yanchor="top", y=1, xanchor="left", x=1.02)
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Personalizar el hovertemplate
|
| 143 |
+
hovertemplate = '<b>%{y}</b>'
|
| 144 |
+
|
| 145 |
+
# Generar el gráfico basado en el tipo seleccionado
|
| 146 |
+
if chart_type == "Línea":
|
| 147 |
+
fig = px.line(data, x="X", y=y_names_list, line_shape="spline")
|
| 148 |
+
fig.update_traces(hovertemplate=hovertemplate)
|
| 149 |
+
if use_multiple_colors:
|
| 150 |
+
for i, name in enumerate(y_names_list):
|
| 151 |
+
fig.update_traces(selector=dict(name=name), line_color=colors[i % len(colors)])
|
| 152 |
+
else:
|
| 153 |
+
fig.update_traces(line_color=color)
|
| 154 |
+
elif chart_type == "Área":
|
| 155 |
+
fig = px.area(data, x="X", y=y_names_list, line_shape="spline")
|
| 156 |
+
fig.update_traces(hovertemplate=hovertemplate)
|
| 157 |
+
if use_multiple_colors:
|
| 158 |
+
for i, name in enumerate(y_names_list):
|
| 159 |
+
fig.update_traces(selector=dict(name=name), line_color=colors[i % len(colors)], fillcolor=colors[i % len(colors)])
|
| 160 |
+
else:
|
| 161 |
+
fig.update_traces(line_color=color, fillcolor=color)
|
| 162 |
+
elif chart_type == "Dispersión":
|
| 163 |
+
fig = px.scatter(data, x="X", y=y_names_list)
|
| 164 |
+
fig.update_traces(hovertemplate=hovertemplate)
|
| 165 |
+
if use_multiple_colors:
|
| 166 |
+
for i, name in enumerate(y_names_list):
|
| 167 |
+
fig.update_traces(selector=dict(name=name), marker_color=colors[i % len(colors)], marker_line_color=border_colors[i % len(border_colors)], marker_line_width=border_width)
|
| 168 |
+
else:
|
| 169 |
+
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 170 |
+
elif chart_type == "Barras":
|
| 171 |
+
if horizontal_bars:
|
| 172 |
+
fig = px.bar(data, x=y_names_list[0], y="X", orientation='h', barmode='stack' if stacked_bars else 'group')
|
| 173 |
+
else:
|
| 174 |
+
fig = px.bar(data, x="X", y=y_names_list, barmode='stack' if stacked_bars else 'group')
|
| 175 |
+
fig.update_traces(hovertemplate=hovertemplate)
|
| 176 |
+
if use_multiple_colors:
|
| 177 |
+
if len(y_names_list) == 1: # Solo una variable Y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
fig.update_traces(marker_color=colors[:len(x)], marker_line_color=border_colors[:len(x)], marker_line_width=border_width)
|
| 179 |
else:
|
| 180 |
+
for i, trace in enumerate(fig.data):
|
| 181 |
+
trace.marker.color = colors[i % len(colors)]
|
| 182 |
+
trace.marker.line.color = border_colors[i % len(border_colors)]
|
| 183 |
+
trace.marker.line.width = border_width
|
| 184 |
+
else:
|
| 185 |
+
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 186 |
+
fig.update_layout(bargap=0.2)
|
| 187 |
+
elif chart_type == "Donut":
|
| 188 |
+
figs = []
|
| 189 |
+
for i, y_name in enumerate(y_names_list):
|
| 190 |
+
fig = px.pie(data, values=y_name, names="X", hole=hole_size, color_discrete_sequence=colors[:len(x)])
|
| 191 |
+
fig.update_traces(hovertemplate='<b>%{label}</b>: %{value} (%{percent})', textinfo='percent+label')
|
| 192 |
fig.update_layout(
|
| 193 |
title=dict(
|
| 194 |
+
text=f"{chart_title}<br><sub>{y_name}</sub>" if len(y_names_list) > 1 else chart_title,
|
| 195 |
x=0.5,
|
| 196 |
y=0.95,
|
| 197 |
xanchor='center',
|
|
|
|
| 202 |
color="#374151",
|
| 203 |
weight="normal"
|
| 204 |
)
|
| 205 |
+
),
|
| 206 |
+
**common_layout
|
| 207 |
)
|
| 208 |
+
figs.append(fig)
|
| 209 |
+
for fig in figs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
st.plotly_chart(fig)
|
| 211 |
|
| 212 |
+
# Añadir anotación para el título
|
| 213 |
+
if chart_type != "Donut":
|
| 214 |
+
fig.update_layout(
|
| 215 |
+
title=dict(
|
| 216 |
+
text=f"{chart_title}",
|
| 217 |
+
x=0.5,
|
| 218 |
+
y=0.95,
|
| 219 |
+
xanchor='center',
|
| 220 |
+
yanchor='top',
|
| 221 |
+
font=dict(
|
| 222 |
+
family=font_family,
|
| 223 |
+
size=18,
|
| 224 |
+
color="#374151",
|
| 225 |
+
weight="normal"
|
| 226 |
+
)
|
| 227 |
+
)
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Aplicar configuración común
|
| 231 |
+
fig.update_layout(**common_layout)
|
| 232 |
+
|
| 233 |
+
# Aplicar múltiples colores si se seleccionó la opción
|
| 234 |
+
if use_multiple_colors and chart_type != "Donut":
|
| 235 |
+
for i, trace in enumerate(fig.data):
|
| 236 |
+
trace.update(marker_color=colors[i % len(colors)], marker_line_color=border_colors[i % len(border_colors)], marker_line_width=border_width)
|
| 237 |
+
|
| 238 |
+
# Mostrar el gráfico
|
| 239 |
+
st.plotly_chart(fig)
|
| 240 |
+
|
| 241 |
+
# Forzar el autoescale
|
| 242 |
+
fig.update_layout(xaxis_autorange=True, yaxis_autorange=True)
|
| 243 |
|
| 244 |
# Información adicional
|
| 245 |
st.write("Aquí puede añadir información adicional sobre los gráficos generados.")
|