Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import plotly.express as px
|
| 3 |
-
import plotly.graph_objects as go
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
|
|
@@ -43,31 +42,22 @@ st.sidebar.header("Configuración del Gráfico")
|
|
| 43 |
# Título del gráfico
|
| 44 |
chart_title = st.sidebar.text_input("Título del Gráfico", "Generador de Gráfico")
|
| 45 |
|
| 46 |
-
# Tipo de gráfico
|
| 47 |
-
chart_type = st.sidebar.selectbox("Tipo de Gráfico", ["Línea", "Área", "Dispersión", "Barras", "Donut", "Radar"])
|
| 48 |
-
|
| 49 |
# Ingresar valores para los ejes
|
| 50 |
x_values = st.sidebar.text_area("Valores para X (separados por comas)", "2013,2014,2015,2016,2017,2018")
|
| 51 |
x = x_values.split(",")
|
| 52 |
|
| 53 |
# Selector de número de variables Y
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
y_names_list.append(y_name)
|
| 66 |
-
else:
|
| 67 |
-
y_values = st.sidebar.text_area("Valores para Y (separados por comas)", "10,18,5,9,12,21")
|
| 68 |
-
y_name = st.sidebar.text_input("Nombre de la Variable Y", "Variable Y")
|
| 69 |
-
y_values_list = [y_values]
|
| 70 |
-
y_names_list = [y_name]
|
| 71 |
|
| 72 |
# Etiquetas personalizadas para los ejes
|
| 73 |
x_label = st.sidebar.text_input("Etiqueta para el eje X", "X")
|
|
@@ -75,17 +65,19 @@ y_label = st.sidebar.text_input("Etiqueta para el eje Y", "Y")
|
|
| 75 |
|
| 76 |
# Desplegable de opciones adicionales
|
| 77 |
with st.sidebar.expander("Opciones Adicionales"):
|
|
|
|
| 78 |
graph_width = st.slider("Ancho del Gráfico", min_value=400, max_value=1000, value=800, step=50)
|
| 79 |
graph_height = st.slider("Alto del Gráfico", min_value=300, max_value=800, value=600, step=50)
|
| 80 |
font_family = st.selectbox("Fuente", font_options, index=font_options.index("Times New Roman"))
|
| 81 |
show_legend = st.checkbox("Mostrar Leyenda", value=True)
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# Opción para múltiples colores
|
| 84 |
use_multiple_colors = st.sidebar.checkbox("Usar múltiples colores")
|
| 85 |
|
| 86 |
# Seleccionar color(es) para el gráfico
|
| 87 |
selected_color = st.sidebar.color_picker("Color", "#24CBA0", key="single_color")
|
| 88 |
-
opacity = st.sidebar.slider("Opacidad (%)", min_value=0, max_value=100, value=30, step=1) / 100
|
| 89 |
|
| 90 |
# Selector de tamaño del agujero para el gráfico de donuts
|
| 91 |
if chart_type == "Donut":
|
|
@@ -98,11 +90,11 @@ if use_multiple_colors:
|
|
| 98 |
num_colors = max(len(y_values_list), len(x))
|
| 99 |
colors = [hex_to_rgba(selected_color if i == 0 else st.sidebar.color_picker(f"Color {i+1}", predefined_colors[i % len(predefined_colors)], key=f"color_{i}"), alpha=opacity)
|
| 100 |
for i in range(num_colors)]
|
| 101 |
-
border_colors = [hex_to_rgba(selected_color if i == 0 else predefined_colors[i % len(predefined_colors)], alpha=
|
| 102 |
for i in range(num_colors)]
|
| 103 |
else:
|
| 104 |
color = hex_to_rgba(selected_color, alpha=opacity)
|
| 105 |
-
border_color = hex_to_rgba(selected_color, alpha=
|
| 106 |
|
| 107 |
# Procesar valores
|
| 108 |
y_values_lists = [[float(i) for i in y_values.split(",") if i.strip()] for y_values in y_values_list]
|
|
@@ -165,9 +157,9 @@ else:
|
|
| 165 |
fig.update_traces(hovertemplate=hovertemplate)
|
| 166 |
if use_multiple_colors:
|
| 167 |
for i, name in enumerate(y_names_list):
|
| 168 |
-
fig.update_traces(selector=dict(name=name), marker_color=colors[i % len(colors)], marker_line_color=border_colors[i % len(border_colors)])
|
| 169 |
else:
|
| 170 |
-
fig.update_traces(marker_color=color)
|
| 171 |
elif chart_type == "Barras":
|
| 172 |
if num_y_vars > 1:
|
| 173 |
if bar_mode == "Normal":
|
|
@@ -177,9 +169,9 @@ else:
|
|
| 177 |
else:
|
| 178 |
fig = px.bar(data, x="X", y=y_names_list)
|
| 179 |
if use_multiple_colors:
|
| 180 |
-
fig.update_traces(marker_color=colors[:len(x)], marker_line_color=border_colors[:len(x)], marker_line_width=
|
| 181 |
else:
|
| 182 |
-
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=
|
| 183 |
fig.update_traces(hovertemplate=hovertemplate)
|
| 184 |
fig.update_layout(bargap=0.2) # Ajustar el espacio entre las barras
|
| 185 |
elif chart_type == "Donut":
|
|
@@ -192,43 +184,6 @@ else:
|
|
| 192 |
figs.append(fig)
|
| 193 |
for fig in figs:
|
| 194 |
st.plotly_chart(fig)
|
| 195 |
-
elif chart_type == "Radar":
|
| 196 |
-
fig = go.Figure()
|
| 197 |
-
|
| 198 |
-
for i, y_name in enumerate(y_names_list):
|
| 199 |
-
fig.add_trace(go.Scatterpolar(
|
| 200 |
-
r=y_values_lists[i],
|
| 201 |
-
theta=x,
|
| 202 |
-
fill='toself',
|
| 203 |
-
name=y_name,
|
| 204 |
-
line=dict(color=colors[i % len(colors)] if use_multiple_colors else color)
|
| 205 |
-
))
|
| 206 |
-
|
| 207 |
-
# Configuramos el diseño del gráfico
|
| 208 |
-
fig.update_layout(
|
| 209 |
-
polar=dict(
|
| 210 |
-
radialaxis=dict(
|
| 211 |
-
visible=True,
|
| 212 |
-
range=[0, max(max(y) for y in y_values_lists)], # Usar el máximo valor como rango
|
| 213 |
-
showticklabels=True,
|
| 214 |
-
linewidth=1,
|
| 215 |
-
gridcolor='lightgray'
|
| 216 |
-
),
|
| 217 |
-
angularaxis=dict(
|
| 218 |
-
showticklabels=True,
|
| 219 |
-
linewidth=1,
|
| 220 |
-
gridcolor='lightgray'
|
| 221 |
-
)
|
| 222 |
-
),
|
| 223 |
-
showlegend=show_legend,
|
| 224 |
-
width=graph_width,
|
| 225 |
-
height=graph_height,
|
| 226 |
-
font=dict(family=font_family, size=18, color="black"),
|
| 227 |
-
title=chart_title,
|
| 228 |
-
margin=dict(l=60, r=40, t=100, b=40),
|
| 229 |
-
)
|
| 230 |
-
|
| 231 |
-
st.plotly_chart(fig)
|
| 232 |
|
| 233 |
# Añadir anotación para el título
|
| 234 |
if chart_type not in ["Donut"]:
|
|
@@ -260,7 +215,7 @@ else:
|
|
| 260 |
# Aplicar múltiples colores si se seleccionó la opción
|
| 261 |
if use_multiple_colors and chart_type not in ["Donut"] and not (chart_type == "Barras" and num_y_vars == 1):
|
| 262 |
for i, trace in enumerate(fig.data):
|
| 263 |
-
trace.update(marker_color=colors[i % len(colors)], marker_line_color=border_colors[i % len(border_colors)], marker_line_width=
|
| 264 |
elif use_multiple_colors and chart_type in ["Donut"]:
|
| 265 |
fig.update_traces(marker=dict(colors=colors))
|
| 266 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import plotly.express as px
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
|
|
|
|
| 42 |
# Título del gráfico
|
| 43 |
chart_title = st.sidebar.text_input("Título del Gráfico", "Generador de Gráfico")
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
# Ingresar valores para los ejes
|
| 46 |
x_values = st.sidebar.text_area("Valores para X (separados por comas)", "2013,2014,2015,2016,2017,2018")
|
| 47 |
x = x_values.split(",")
|
| 48 |
|
| 49 |
# Selector de número de variables Y
|
| 50 |
+
num_y_vars = st.sidebar.number_input("Número de variables Y", min_value=1, max_value=100, value=1, step=1)
|
| 51 |
+
y_values_list = []
|
| 52 |
+
y_names_list = []
|
| 53 |
+
for i in range(num_y_vars):
|
| 54 |
+
if f"y_values_{i}" not in st.session_state:
|
| 55 |
+
st.session_state[f"y_values_{i}"] = ','.join([str(np.random.randint(1, 25)) for _ in x])
|
| 56 |
+
y_values = st.sidebar.text_area(f"Valores para Y-{i+1} (separados por comas)", st.session_state[f"y_values_{i}"])
|
| 57 |
+
st.session_state[f"y_values_{i}"] = y_values # Guardar los valores en session_state
|
| 58 |
+
y_name = st.sidebar.text_input(f"Nombre de la Variable Y-{i+1}", f"Variable Y-{i+1}")
|
| 59 |
+
y_values_list.append(y_values)
|
| 60 |
+
y_names_list.append(y_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Etiquetas personalizadas para los ejes
|
| 63 |
x_label = st.sidebar.text_input("Etiqueta para el eje X", "X")
|
|
|
|
| 65 |
|
| 66 |
# Desplegable de opciones adicionales
|
| 67 |
with st.sidebar.expander("Opciones Adicionales"):
|
| 68 |
+
chart_type = st.selectbox("Tipo de Gráfico", ["Línea", "Área", "Dispersión", "Barras", "Donut"])
|
| 69 |
graph_width = st.slider("Ancho del Gráfico", min_value=400, max_value=1000, value=800, step=50)
|
| 70 |
graph_height = st.slider("Alto del Gráfico", min_value=300, max_value=800, value=600, step=50)
|
| 71 |
font_family = st.selectbox("Fuente", font_options, index=font_options.index("Times New Roman"))
|
| 72 |
show_legend = st.checkbox("Mostrar Leyenda", value=True)
|
| 73 |
+
opacity = st.slider("Opacidad (%)", min_value=0, max_value=100, value=30, step=1) / 100
|
| 74 |
+
border_width = st.slider("Grosor del Borde", min_value=0.0, max_value=3.0, value=1.5, step=0.1)
|
| 75 |
|
| 76 |
# Opción para múltiples colores
|
| 77 |
use_multiple_colors = st.sidebar.checkbox("Usar múltiples colores")
|
| 78 |
|
| 79 |
# Seleccionar color(es) para el gráfico
|
| 80 |
selected_color = st.sidebar.color_picker("Color", "#24CBA0", key="single_color")
|
|
|
|
| 81 |
|
| 82 |
# Selector de tamaño del agujero para el gráfico de donuts
|
| 83 |
if chart_type == "Donut":
|
|
|
|
| 90 |
num_colors = max(len(y_values_list), len(x))
|
| 91 |
colors = [hex_to_rgba(selected_color if i == 0 else st.sidebar.color_picker(f"Color {i+1}", predefined_colors[i % len(predefined_colors)], key=f"color_{i}"), alpha=opacity)
|
| 92 |
for i in range(num_colors)]
|
| 93 |
+
border_colors = [hex_to_rgba(selected_color if i == 0 else predefined_colors[i % len(predefined_colors)], alpha=opacity)
|
| 94 |
for i in range(num_colors)]
|
| 95 |
else:
|
| 96 |
color = hex_to_rgba(selected_color, alpha=opacity)
|
| 97 |
+
border_color = hex_to_rgba(selected_color, alpha=opacity)
|
| 98 |
|
| 99 |
# Procesar valores
|
| 100 |
y_values_lists = [[float(i) for i in y_values.split(",") if i.strip()] for y_values in y_values_list]
|
|
|
|
| 157 |
fig.update_traces(hovertemplate=hovertemplate)
|
| 158 |
if use_multiple_colors:
|
| 159 |
for i, name in enumerate(y_names_list):
|
| 160 |
+
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)
|
| 161 |
else:
|
| 162 |
+
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 163 |
elif chart_type == "Barras":
|
| 164 |
if num_y_vars > 1:
|
| 165 |
if bar_mode == "Normal":
|
|
|
|
| 169 |
else:
|
| 170 |
fig = px.bar(data, x="X", y=y_names_list)
|
| 171 |
if use_multiple_colors:
|
| 172 |
+
fig.update_traces(marker_color=colors[:len(x)], marker_line_color=border_colors[:len(x)], marker_line_width=border_width)
|
| 173 |
else:
|
| 174 |
+
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 175 |
fig.update_traces(hovertemplate=hovertemplate)
|
| 176 |
fig.update_layout(bargap=0.2) # Ajustar el espacio entre las barras
|
| 177 |
elif chart_type == "Donut":
|
|
|
|
| 184 |
figs.append(fig)
|
| 185 |
for fig in figs:
|
| 186 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# Añadir anotación para el título
|
| 189 |
if chart_type not in ["Donut"]:
|
|
|
|
| 215 |
# Aplicar múltiples colores si se seleccionó la opción
|
| 216 |
if use_multiple_colors and chart_type not in ["Donut"] and not (chart_type == "Barras" and num_y_vars == 1):
|
| 217 |
for i, trace in enumerate(fig.data):
|
| 218 |
+
trace.update(marker_color=colors[i % len(colors)], marker_line_color=border_colors[i % len(border_colors)], marker_line_width=border_width)
|
| 219 |
elif use_multiple_colors and chart_type in ["Donut"]:
|
| 220 |
fig.update_traces(marker=dict(colors=colors))
|
| 221 |
|