Reputation: 11
I don't understand why my graphs are not showing up. When I introduce 2 values, capacity and efficiency, the graphs are supposed to show up in my Bokeh interface. Can you tell me why this doesn't happen? I launch the server in anaconda prompt with the command bokeh serve --show app.py
from bokeh.io import curdoc
from bokeh.layouts import column, gridplot
from bokeh.models import ColumnDataSource, TextInput, Button
from bokeh.plotting import figure
from datetime import datetime
class Bateria:
def __init__(self, capacidade_maxima, eficiencia):
self.capacidade_maxima = capacidade_maxima
self.carga_atual = 0
self.eficiencia = eficiencia
def carregar(self, energia_ev):
potencia_carregada = max(0, (self.capacidade_maxima - self.carga_atual) * self.eficiencia)
self.carga_atual += potencia_carregada
def descarregar(self, energia_ev):
potencia_descarregada = max(0, energia_ev - 88)
self.carga_atual -= potencia_descarregada
self.carga_atual = max(0, self.carga_atual)
def nivel_de_carga(self):
return self.carga_atual
dados = [0, 0, 0, 0, 0, 0, 11, 110, 33, 121, 22, 77, 132, 66, 99, 44, 88, 33, 22, 11, 0, 0, 0, 0]
precos_eletricidade = [0.13, 0.11, 0.12, 0.07, 0.14, 0.12, 0.15, 0.16, 0.18, 0.20, 0.25, 0.26, 0.28, 0.29, 0.30, 0.29, 0.28, 0.27, 0.26, 0.25, 0.23, 0.24, 0.20, 0.16, 0.14]
tempo = [f"{i}:00" for i in range(24)]
source_dados = ColumnDataSource(data=dict(x=tempo, y=dados))
source_cargas_bateria = ColumnDataSource(data=dict(x=tempo, y=[0]*24))
source_consumo_total = ColumnDataSource(data=dict(x=tempo, y=[0]*24))
# Widgets
capacity_input = TextInput(value="50", title="Capacidade Máxima da Bateria (kW):")
efficiency_input = TextInput(value="90", title="Eficiência da Bateria (%):")
update_button = Button(label="Atualizar Gráficos")
def update_plots():
try:
capacidade_maxima = int(capacity_input.value)
eficiencia = int(efficiency_input.value) / 100
bateria = Bateria(capacidade_maxima, eficiencia)
cargas_bateria = []
consumo_total = []
dados_atualizados = []
for i, energia_ev in enumerate(dados):
if i <= 6:
preco_atual = precos_eletricidade[i]
if preco_atual == min(precos_eletricidade[:6]):
bateria.carregar(capacidade_maxima)
if i > 6 and bateria.carga_atual < bateria.capacidade_maxima and energia_ev <= 88 and bateria.carga_atual + energia_ev < 90:
bateria.carregar(energia_ev)
soc = bateria.carga_atual / bateria.capacidade_maxima
if energia_ev > 88 and soc > 0.2:
bateria.descarregar(energia_ev)
soc = bateria.carga_atual / bateria.capacidade_maxima
cargas_bateria.append(bateria.nivel_de_carga())
consumo_total.append(energia_ev + bateria.nivel_de_carga())
dados_atualizados.append(energia_ev)
source_dados.data = dict(x=tempo, y=dados_atualizados)
source_cargas_bateria.data = dict(x=tempo, y=cargas_bateria)
source_consumo_total.data = dict(x=tempo, y=consumo_total)
except ValueError as e:
print(e)
update_button.on_click(update_plots)
plot_dados = figure(title="Consumo da Estação ao Longo de 24 Horas", x_axis_label="Tempo (horas)", y_axis_label="Energia (kWh)")
plot_dados.vbar(x='x', top='y', width=0.4, source=source_dados, color="red")
plot_cargas_bateria = figure(title="Evolução da Energia da Bateria ao Longo de 24 Horas", x_axis_label="Tempo (horas)", y_axis_label="Energia (kWh)")
plot_cargas_bateria.vbar(x='x', top='y', width=0.4, source=source_cargas_bateria, color="blue")
plot_consumo_total = figure(title="Consumo da Estação + Energia da Bateria ao Longo de 24 Horas", x_axis_label="Tempo (horas)", y_axis_label="Energia Total (kWh)")
plot_consumo_total.vbar(x='x', top='y', width=0.4, source=source_consumo_total, color="green")
layout = column(capacity_input, efficiency_input, update_button, gridplot([[plot_dados, plot_cargas_bateria, plot_consumo_total]]))
curdoc().add_root(layout)
Do I have some error in the code? Maybe in the plot part?
Upvotes: 0
Views: 66
Reputation: 34568
You are using strings for the x-vlaues:
tempo = [f"{i}:00" for i in range(24)]
If this is actually what you intend, then you will need to explicitly configure a categorical range with all the expected factors:
https://docs.bokeh.org/en/latest/docs/user_guide/topics/categorical.html
Categorical factors are, by definition, completely arbitrary, so you have to be the person to state which ones comprise the range.
Otherwise, if you are actually trying to have a datetime axis, then you need to convert the x-values to actual datetime values (not strings) and configure a datetime axis:
https://docs.bokeh.org/en/latest/docs/user_guide/basic/axes.html#datetime-axes
Upvotes: 1