Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
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
ene | 203174,0357 |
feb | 135202,8519 |
mar | 45120 |
ago | 459425 |
Etiquetas de fila Suma de Ingresos
ene | 5688873 |
feb | 3650477 |
mar | 45120 |
ago | 459425 |
(Tablas generadas dinamicamente del dataset)
Lo que deberia generar el siguiente gráfico:
Pero, se obtiene el de acontinuación:
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
Solved! Go to Solution.
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.
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.
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.
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
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
144 | |
76 | |
63 | |
51 | |
48 |
User | Count |
---|---|
204 | |
86 | |
64 | |
59 | |
56 |