Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Maty_B
Regular Visitor

Problema con python visual (gráfico)

Hola comunidad

Hace unos días que he estado tratando de generar un gráfico de ingresos vs costos, tengo un conjunto de datos con los siguientes atributos

-'Fecha'

-'Cantidad'

-'Ingresos'

-'Costos'

-'Fecha - Mes'

-'Fecha - Número Mes'

El problema es que cuando agrupo el dataset por el atributo 'Fecha - Num Mes' para obtener la media de las demás características (Ingresos y Costos) como se muestra a continuación:

 

 

 

dataset_agrupado = dataset.groupby('Fecha - Mes').mean()

lista_meses = dataset_agrupado.index
lista_num_meses = dataset_agrupado['Fecha - Num Mes']
lista_meses_ingresos = dataset_agrupado['Ingresos']
lista_meses_costos = dataset_agrupado['Costos']

 

 

 

Al momento de gráficar las listas 'lista_meses_ingresos' y 'lista_meses_costos' el resultado obtenido no es la media, sino que su suma, como se muestra en las siguientes tablas

Etiquetas de fila - Promedio de Ingresos

ene203174,0357
feb135202,8519
mar45120
ago459425

Etiquetas de fila Suma de Ingresos

ene5688873
feb3650477
mar45120
ago459425

(Tablas generadas dinamicamente del dataset)

Lo que deberia generar el siguiente gráfico:

Maty_B_1-1667166891733.png

Pero, se obtiene el de acontinuación:

Maty_B_2-1667166925241.png

La codificación utilizada en Power Bi es la siguiente

 

 

# dataset = pandas.DataFrame(Ingresos, Costos, Fecha - Mes, Fecha - Num Mes)
# dataset = dataset.drop_duplicates()

# Importar librerias
from datetime import datetime
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from operator import itemgetter
import numpy as np

# Construcción del marco principal
fig, ax = plt.subplots(dpi=100, figsize=(5, 2.8), facecolor='#FFFFFF')

# Encontrar la lista de las características objetivo (identificador de mes, acumulada de ingresos, acumulada de costos y su frecuencia) para obtener datos medios de los meses
dataset_agrupado = dataset.groupby('Fecha - Mes').mean()

lista_num_meses = dataset_agrupado['Fecha - Num Mes']
lista_meses = dataset_agrupado.index
lista_meses_ingresos = dataset_agrupado['Ingresos']
lista_meses_costos = dataset_agrupado['Costos']
lista_caracteristicas = [[lista_num_meses[i], lista_meses[i], lista_meses_ingresos[i], lista_meses_costos[i]] for i in range(len(lista_num_meses))]

lista_caracteristicas.sort(key=itemgetter(0), reverse=False)

# Generar las funciones/rangos iniciales para dibujar el gráfico
eje_x01 = [x for x in range(len(lista_caracteristicas))]
eje_y01 = [lista_caracteristicas[x][2] for x in range(len(lista_caracteristicas))]
eje_x02 = [x for x in range(len(lista_caracteristicas))]
eje_y02 = [lista_caracteristicas[x][3] for x in range(len(lista_caracteristicas))]

# Suavizar -si es posible- las funciones/rangos de las rectas de ingresos y costos
if len(eje_x01) < 4:
    modelo_interpolacion01 = interp1d(eje_x01, eje_y01, kind="linear")
    modelo_interpolacion02 = interp1d(eje_x02, eje_y02, kind='linear')
else:
    modelo_interpolacion01 = interp1d(eje_x01, eje_y01, kind="cubic")
    modelo_interpolacion02 = interp1d(eje_x02, eje_y02, kind='cubic')

x01 = np.linspace(np.min(eje_x01), np.max(eje_x01), 500)
y01 = modelo_interpolacion01(x01)
x02 = np.linspace(np.min(eje_x02), np.max(eje_x02), 500)
y02 = modelo_interpolacion02(x02)

# Crear las lineas de las funciones
line_x01, = ax.plot(x01, y01, color='#54AFA5', label='Ingresos')
line_x02, = ax.plot(x02, y02, color='#C87286', label='Costos')

# Generar etiquetas para el gráfico
rango_eje_x  = [i for i in range(len(lista_caracteristicas))]
etiqueta_eje_x = [lista_caracteristicas[i][1] for i in rango_eje_x]
max_eje_y = np.max([np.max(lista_meses_ingresos), np.max(lista_meses_costos)])
min_eje_y = np.min([np.min(lista_meses_ingresos), np.min(lista_meses_costos)])
rango_eje_y = [i for i in range(int(min_eje_y), int(max_eje_y) + 1, int((max_eje_y - min_eje_y)/6))]
etiqueta_eje_y = [i for i in rango_eje_y]

# Dibujar nuevas etiquetas al gráfico
ax.set_xticks(rango_eje_x,etiqueta_eje_x)
ax.set_yticks(rango_eje_y,etiqueta_eje_y)
# Pintar el área entre las rectas
ax.fill(np.append(x01, x02[::-1]), np.append(y01, y02[::-1]), '#F5F5F5')

ax.spines['bottom'].set_color('#CCCCCC')
for spine in ['right', 'top', 'left']:
    ax.spines[spine].set_visible(False)
ax.tick_params(width=0, colors='#666666', labelsize=8)
ax.set_facecolor('#FFFFFF')

plt.legend(loc='best', facecolor='#FFFFFF', fontsize=8)

# Mostrar gráfico
plt.show()

 

 

 

Alguna idea del porque sucede este comportamiento? ya que, al momento de ejecutarlo en python 3.7 (pycharm) va todo normal, pero al codificar dentro de PB ocurre lo mencionado anteriormente

1 ACCEPTED SOLUTION
v-tangjie-msft
Community Support
Community Support

Hi @Maty_B ,

 

Drag and drop the attributes/fields to be visualized for analysis using Python in the Values section, as shown in the image. The fields that are added to the Values section shall be available for your Python scripts. Choose the option of “Average” to get the average value.

 

vtangjiemsft_0-1667185203716.pngvtangjiemsft_2-1667185225195.png

 

 

You can refer to the following documents that may be helpful to you:

Matplotlib Tutorial : A Basic Guide to Use Matplotlib with Python (datasciencelearner.com)

Create Power BI visuals using Python in Power BI Desktop - Power BI | Microsoft Learn

 

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

View solution in original post

2 REPLIES 2
v-tangjie-msft
Community Support
Community Support

Hi @Maty_B ,

 

Drag and drop the attributes/fields to be visualized for analysis using Python in the Values section, as shown in the image. The fields that are added to the Values section shall be available for your Python scripts. Choose the option of “Average” to get the average value.

 

vtangjiemsft_0-1667185203716.pngvtangjiemsft_2-1667185225195.png

 

 

You can refer to the following documents that may be helpful to you:

Matplotlib Tutorial : A Basic Guide to Use Matplotlib with Python (datasciencelearner.com)

Create Power BI visuals using Python in Power BI Desktop - Power BI | Microsoft Learn

 

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

Perfect! works great, thank you very much

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.