Spaces:
Configuration error
Configuration error
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Jan 4 18:23:14 2022 | |
@author: bullm | |
""" | |
import plotly.graph_objects as go | |
import plotly.express as px | |
import pandas as pd | |
import streamlit as st | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# path = 'C:\\Users\\bullm\\Desktop\\Portal_LVAM\\Data\\' | |
# for key in stats_competencia.keys(): | |
# print(key) | |
# stats_competencia[key].to_excel(path+key+'.xlsx') | |
# tabla_factores_general.to_excel(path+'factores_general.xlsx') | |
def spider_plot(df, title, alpha=0.1, n_dec=1, y=[]): | |
if len(y) == 0: | |
min_val = df.min().min() | |
min_val = np.floor(min_val-np.abs(min_val)*alpha, n_dec) | |
max_val = df.max().max() | |
max_val = np.ceil(max_val+np.abs(max_val)*alpha, n_dec) | |
mean_val = np.round((min_val + max_val)/2, 1) | |
y = [min_val, mean_val, max_val] | |
categories = list(df.index) | |
N = df.shape[0] | |
# We are going to plot the first line of the data frame. | |
# But we need to repeat the first value to close the circular graph: | |
values = np.round(df.iloc[:, 0].values.flatten().tolist(), 2) | |
values = np.hstack((values, values[0])).T | |
values2 = np.round(df.iloc[:, 1].values.flatten().tolist(), 2) | |
values2 = np.hstack((values2, values2[0])).T | |
# What will be the angle of each axis in the plot? | |
angles = [n / float(N) * 2 * np.pi for n in range(N)] | |
angles += angles[:1] | |
# Initialise the spider plot | |
fig = plt.figure(figsize=(10, 15)) | |
ax = plt.subplot(111, polar=True) | |
ax.set_title(title, fontsize=20) | |
# Draw one axe per variable + add labels labels yet | |
plt.xticks(angles[:-1], categories, color="black", size=18) | |
# Draw ylabels | |
ax.set_rlabel_position(0) | |
plt.yticks(y, [str(i) for i in y], color="black", size=15) | |
plt.ylim(y[0], y[-1]) | |
# Plot data | |
ax.plot(angles, values, linewidth=1, linestyle='solid', c='dimgrey') | |
ax.plot(angles, values2, linewidth=1, linestyle='solid', c='darkred') | |
handles, labels = ax.get_legend_handles_labels() | |
ax.legend(labels=['Cartera', 'Benchmark'], loc='lower left', | |
fontsize=15, bbox_to_anchor=(-0.1, -0.1)) | |
# Fill area | |
ax.fill(angles, values, 'b', alpha=0.1) | |
return fig | |
def panel_de_control(): | |
df = pd.read_excel("Data/factores_general.xlsx") | |
radar = go.Scatterpolar( | |
r = list(df["Benchmark"]), | |
theta = list(df['Unnamed: 0']), | |
fill = 'toself' | |
) | |
radar2 = go.Scatterpolar( | |
r = list(df["Cartera"]), | |
theta = list(df['Unnamed: 0']), | |
fill = 'toself' | |
) | |
data = [radar, radar2] | |
fig = go.Figure(data = data) | |
col1, col2 = st.columns(2) | |
col1.plotly_chart(fig, use_container_width=True) | |
df.index= df['Unnamed: 0'] | |
df = df[["Cartera", 'Benchmark']] | |
col2.pyplot(spider_plot(df, '', y=[0, 50, 100] )) |