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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
alfertab
Helper I
Helper I

Change color to one column in a stacked bar chart

Hi everybody, I'm having some issues using the stacked bar chart in Power BI. For the moment I'm using  4 measures on the Y axis, this measures are based on the table "Data". My example is below:

 

Tabla named "Data":

Net Base DemandNet Int DemandNet Peak DemandLoad Base DemandLoad Int DemandLoad Peak Demand
14501200500125011501100

 

 

 

Load = 
SWITCH(TRUE(),
SELECTEDVALUE(Horario[Horario]) = "Base", 'Data'[Load Base Demand],
SELECTEDVALUE(Horario[Horario]) = "Intermedio", IF ('Data'[Net Int Demand]>'Data'[Load Int Demand], 'Data'[Load Int Demand],'Data'[Net Int Demand])

Net = 
SWITCH(TRUE(),
SELECTEDVALUE(Horario[Horario]) = "Base", -('Data'[Load Base Demand]-'Data'[Net Base Demand]),
SELECTEDVALUE(Horario[Horario]) = "Intermedio", ABS('Data'[Load Int Demand]-'Data'[Net Int Demand])

Net Chart BESS Peak =
SWITCH(TRUE(),
SELECTEDVALUE(Horario[Horario]) = "Punta", 'Data'[Net Peak Demand])

Load Chart BESS Peak =
SWITCH(TRUE(),
SELECTEDVALUE(Horario[Horario]) = "Punta", [Peak Demand Dif])

 

 

 

 On the X axis, I'm using a column from a table like the following: 

Horario

Base
Intermedio
Punta

 

Using this on the stacked bar chart I got the following result: 

 

alfertab_0-1708529089161.png

 

This is the result I want, I just need to hide the legends called as "Gráfica Net perfil BESS Punta" and the one "Gráfica Load perfil BESS Punta". I also need let the chart continue to show the legends named as "Load" and "Net"

Does anyone know how to do it? 

1 ACCEPTED SOLUTION
alfertab
Helper I
Helper I

Hi everybody, I found a solution to my problem. The solution is use the matplotlib library from python. The code I used was the following:

 

# El código siguiente, que crea un dataframe y quita las filas duplicadas, siempre se ejecuta y actúa como un preámbulo del script: 

# dataset = pandas.DataFrame(undefined, undefined.1, undefined.2, undefined.3)
# dataset = dataset.drop_duplicates()

# Pegue o escriba aquí el código de script:
import matplotlib as mb
import matplotlib.pyplot as plt
import numpy as np

#Hacer bonito el gráfico
mb.rcParams['font.size'] =12
mb.rcParams['text.color'] ="#000000"
mb.rcParams['axes.labelcolor']="white"
mb.rcParams['xtick.color']="white"
mb.rcParams['ytick.color']="white"

#Definir Parámetros
Horario = dataset.loc[0:6,'Horario'].tolist()
Load_base= dataset.loc[0:6,'Grafica Load perfil BESS Base'].tolist()
Net_base = dataset.loc[0:6,'Grafica Net perfil BESS Base'].tolist()
Load_int= dataset.loc[0:6,'Grafica Load perfil BESS Int'].tolist()
Net_int = dataset.loc[0:6,'Grafica Net perfil BESS Int'].tolist()
Load_peak = dataset.loc[0:6,'Gráfica Load perfil BESS punta'].tolist()
Net_peak = dataset.loc[0:6,'Gráfica Net perfil BESS punta'].tolist()

#Formato Condicional para demanda Intermedio
Int_demand_conditional = dataset.loc[0:6,'Intermediate Demand Dif tarjeta']
color_cond_Int_1 = np.where(Int_demand_conditional>0,"#B3B3B3","#FBED1D")
color_cond_Int_2 = np.where(Int_demand_conditional>0, "#FBED1D", "#000000")
color_cond_Int_edgecolor_1 = np.where(Int_demand_conditional>0,"#B3B3B3","#FBED1D")
color_cond_Int_edgecolor_2 = np.where(Int_demand_conditional>0, "#FBED1D", "#B3B3B3")

#Cambiar color de la parte de afuera del fondo
plt.figure(facecolor="#000000")

#Cambiar color de fondo
ax=plt.axes()
ax.set_facecolor("#000000")

#Crear barras 
plt.bar (Horario, Load_base, color ="#b3b3b3", edgecolor="#b3b3b3",linewidth=2  )
plt.bar (Horario, Net_base, bottom = Load_base, color = "#FBED1D", edgecolor="#FBED1D",linewidth=2)
plt.bar (Horario, Load_int, color = color_cond_Int_1, edgecolor=color_cond_Int_edgecolor_1,linewidth=2 )
plt.bar (Horario, Net_int, bottom = Load_int, color = color_cond_Int_2, edgecolor=color_cond_Int_edgecolor_2,linewidth=2 )
plt.bar (Horario,Net_peak, color ="#FBED1D", edgecolor="#FBED1D",linewidth=2  )
plt.bar (Horario,Load_peak, bottom = Net_peak, color ="#000000", edgecolor="#b3b3b3",linewidth=2 )

#Nombrar ejes
plt.xlabel("Horario")
plt.ylabel("Demanda [kW]")

#Poner leyendas
plt.legend(["Load", "Net"])

plt.show()

 

View solution in original post

3 REPLIES 3
alfertab
Helper I
Helper I

Hi everybody, I found a solution to my problem. The solution is use the matplotlib library from python. The code I used was the following:

 

# El código siguiente, que crea un dataframe y quita las filas duplicadas, siempre se ejecuta y actúa como un preámbulo del script: 

# dataset = pandas.DataFrame(undefined, undefined.1, undefined.2, undefined.3)
# dataset = dataset.drop_duplicates()

# Pegue o escriba aquí el código de script:
import matplotlib as mb
import matplotlib.pyplot as plt
import numpy as np

#Hacer bonito el gráfico
mb.rcParams['font.size'] =12
mb.rcParams['text.color'] ="#000000"
mb.rcParams['axes.labelcolor']="white"
mb.rcParams['xtick.color']="white"
mb.rcParams['ytick.color']="white"

#Definir Parámetros
Horario = dataset.loc[0:6,'Horario'].tolist()
Load_base= dataset.loc[0:6,'Grafica Load perfil BESS Base'].tolist()
Net_base = dataset.loc[0:6,'Grafica Net perfil BESS Base'].tolist()
Load_int= dataset.loc[0:6,'Grafica Load perfil BESS Int'].tolist()
Net_int = dataset.loc[0:6,'Grafica Net perfil BESS Int'].tolist()
Load_peak = dataset.loc[0:6,'Gráfica Load perfil BESS punta'].tolist()
Net_peak = dataset.loc[0:6,'Gráfica Net perfil BESS punta'].tolist()

#Formato Condicional para demanda Intermedio
Int_demand_conditional = dataset.loc[0:6,'Intermediate Demand Dif tarjeta']
color_cond_Int_1 = np.where(Int_demand_conditional>0,"#B3B3B3","#FBED1D")
color_cond_Int_2 = np.where(Int_demand_conditional>0, "#FBED1D", "#000000")
color_cond_Int_edgecolor_1 = np.where(Int_demand_conditional>0,"#B3B3B3","#FBED1D")
color_cond_Int_edgecolor_2 = np.where(Int_demand_conditional>0, "#FBED1D", "#B3B3B3")

#Cambiar color de la parte de afuera del fondo
plt.figure(facecolor="#000000")

#Cambiar color de fondo
ax=plt.axes()
ax.set_facecolor("#000000")

#Crear barras 
plt.bar (Horario, Load_base, color ="#b3b3b3", edgecolor="#b3b3b3",linewidth=2  )
plt.bar (Horario, Net_base, bottom = Load_base, color = "#FBED1D", edgecolor="#FBED1D",linewidth=2)
plt.bar (Horario, Load_int, color = color_cond_Int_1, edgecolor=color_cond_Int_edgecolor_1,linewidth=2 )
plt.bar (Horario, Net_int, bottom = Load_int, color = color_cond_Int_2, edgecolor=color_cond_Int_edgecolor_2,linewidth=2 )
plt.bar (Horario,Net_peak, color ="#FBED1D", edgecolor="#FBED1D",linewidth=2  )
plt.bar (Horario,Load_peak, bottom = Net_peak, color ="#000000", edgecolor="#b3b3b3",linewidth=2 )

#Nombrar ejes
plt.xlabel("Horario")
plt.ylabel("Demanda [kW]")

#Poner leyendas
plt.legend(["Load", "Net"])

plt.show()

 

lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

@lbendlin Thanks for the recommendation, I already updated the post. I made a little bit changes from the original because I found a way of edit sepparate bars.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.