Coverage for examples/OneRoom_SimpleMPC/plot_results.py: 99%
99 statements
« prev ^ index » next coverage.py v7.4.4, created at 2026-03-26 09:43 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2026-03-26 09:43 +0000
1import numpy as np
2import matplotlib.pyplot as plt
3import matplotlib
4import agentlib_mpc.utils.plotting.basic as mpcplot
5from agentlib_mpc.utils.analysis import mpc_at_time_step
6from agentlib_flexquant.data_structures.flex_results import Results
7matplotlib.use('Agg')
10def plot_results(results_data: dict = None):
11 """
12 Example how plotting with matplotlib and mpcplot from agentlib_mpc works
13 """
14 if results_data is None:
15 res = Results(
16 flex_config="flex_configs/flexibility_agent_config.json",
17 simulator_agent_config="mpc_and_sim/simple_sim.json",
18 results="results"
19 )
20 else:
21 res = Results(
22 flex_config="flex_configs/flexibility_agent_config.json",
23 simulator_agent_config="mpc_and_sim/simple_sim.json",
24 results=results_data
25 )
27 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=2)
28 (ax1, ax2) = axs
29 # load
30 ax1.set_ylabel(r"$\dot{Q}_{Room}$ in W")
31 res.df_simulation["load"].dropna().plot(ax=ax1)
32 # T_in
33 ax2.set_ylabel("$T_{in}$ in K")
34 res.df_simulation["T_in"].dropna().plot(ax=ax2)
35 x_ticks = np.arange(0, 3600 * 6 + 1, 3600)
36 x_tick_labels = [int(tick / 3600) for tick in x_ticks]
37 ax2.set_xticks(x_ticks)
38 ax2.set_xticklabels(x_tick_labels)
39 ax2.set_xlabel("Time in hours")
40 for ax in axs:
41 mpcplot.make_grid(ax)
42 ax.set_xlim(0, 3600 * 6)
44 # room temp
45 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=1)
46 ax1 = axs[0]
47 # T out
48 ax1.set_ylabel("$T_{room}$ in K")
49 res.df_simulation["T_upper"].dropna().plot(ax=ax1, color="0.5")
50 res.df_simulation["T_lower"].dropna().plot(ax=ax1, color="0.5")
51 res.df_simulation["T_out"].dropna().plot(ax=ax1, color=mpcplot.EBCColors.dark_grey)
52 mpc_at_time_step(
53 data=res.df_neg_flex, time_step=9000, variable="T"
54 ).plot(ax=ax1, label="neg", linestyle="--", color=mpcplot.EBCColors.red)
55 mpc_at_time_step(
56 data=res.df_pos_flex, time_step=9000, variable="T"
57 ).plot(ax=ax1, label="pos", linestyle="--", color=mpcplot.EBCColors.blue)
58 mpc_at_time_step(
59 data=res.df_baseline, time_step=9900, variable="T"
60 ).plot(ax=ax1, label="base", linestyle="--", color=mpcplot.EBCColors.dark_grey)
62 ax1.legend()
63 ax1.vlines(9000, ymin=0, ymax=500, colors="black")
64 ax1.vlines(9900, ymin=0, ymax=500, colors="black")
65 ax1.vlines(10800, ymin=0, ymax=500, colors="black")
66 ax1.vlines(18000, ymin=0, ymax=500, colors="black")
68 ax1.set_ylim(289, 299)
69 x_ticks = np.arange(0, 3600 * 6 + 1, 3600)
70 x_tick_labels = [int(tick / 3600) for tick in x_ticks]
71 ax1.set_xticks(x_ticks)
72 ax1.set_xticklabels(x_tick_labels)
73 ax1.set_xlabel("Time in hours")
74 for ax in axs:
75 mpcplot.make_grid(ax)
76 ax.set_xlim(0, 3600 * 6)
78 # predictions
79 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=2)
80 (ax1, ax2) = axs
81 # P_el
82 ax1.set_ylabel("$P_{el}$ in kW")
83 res.df_simulation["P_el"].dropna().plot(ax=ax1, color=mpcplot.EBCColors.dark_grey)
84 mpc_at_time_step(
85 data=res.df_neg_flex, time_step=9000, variable="P_el"
86 ).ffill().plot(
87 ax=ax1,
88 drawstyle="steps-post",
89 label="neg",
90 linestyle="--",
91 color=mpcplot.EBCColors.red,
92 )
93 mpc_at_time_step(
94 data=res.df_pos_flex, time_step=9000, variable="P_el"
95 ).ffill().plot(
96 ax=ax1,
97 drawstyle="steps-post",
98 label="pos",
99 linestyle="--",
100 color=mpcplot.EBCColors.blue,
101 )
102 mpc_at_time_step(
103 data=res.df_baseline, time_step=9000, variable="P_el"
104 ).ffill().plot(
105 ax=ax1,
106 drawstyle="steps-post",
107 label="base",
108 linestyle="--",
109 color=mpcplot.EBCColors.dark_grey,
110 )
111 ax1.legend()
112 ax1.vlines(9000, ymin=-1000, ymax=5000, colors="black")
113 ax1.vlines(9900, ymin=-1000, ymax=5000, colors="black")
114 ax1.vlines(10800, ymin=-1000, ymax=5000, colors="black")
115 ax1.vlines(18000, ymin=-1000, ymax=5000, colors="black")
116 ax1.set_ylim(-0.1, 1)
118 # mdot
119 ax2.set_ylabel(r"$\dot{m}$ in kg/s")
120 res.df_simulation["mDot"].dropna().plot(ax=ax2, color=mpcplot.EBCColors.dark_grey)
121 mpc_at_time_step(
122 data=res.df_neg_flex, time_step=9000, variable="mDot"
123 ).ffill().plot(
124 ax=ax2,
125 drawstyle="steps-post",
126 label="neg",
127 linestyle="--",
128 color=mpcplot.EBCColors.red,
129 )
130 mpc_at_time_step(
131 data=res.df_pos_flex, time_step=9000, variable="mDot"
132 ).ffill().plot(
133 ax=ax2,
134 drawstyle="steps-post",
135 label="pos",
136 linestyle="--",
137 color=mpcplot.EBCColors.blue,
138 )
139 mpc_at_time_step(
140 data=res.df_baseline, time_step=9900, variable="mDot"
141 ).ffill().plot(
142 ax=ax2,
143 drawstyle="steps-post",
144 label="base",
145 linestyle="--",
146 color=mpcplot.EBCColors.dark_grey,
147 )
148 ax2.legend()
149 ax2.vlines(9000, ymin=0, ymax=500, colors="black")
150 ax2.vlines(9900, ymin=0, ymax=500, colors="black")
151 ax2.vlines(10800, ymin=0, ymax=500, colors="black")
152 ax2.vlines(18000, ymin=0, ymax=500, colors="black")
154 ax2.set_ylim(0, 0.06)
156 x_ticks = np.arange(0, 3600 * 6 + 1, 3600)
157 x_tick_labels = [int(tick / 3600) for tick in x_ticks]
158 ax2.set_xticks(x_ticks)
159 ax2.set_xticklabels(x_tick_labels)
160 ax2.set_xlabel("Time in hours")
161 for ax in axs:
162 mpcplot.make_grid(ax)
163 ax.set_xlim(0, 3600 * 6)
165 # flexibility
166 # get only the first prediction time of each time step
167 energy_flex_neg = res.df_indicator.xs("negative_energy_flex", axis=1).droplevel(1).dropna()
168 energy_flex_pos = res.df_indicator.xs("positive_energy_flex", axis=1).droplevel(1).dropna()
169 fig, axs = mpcplot.make_fig(style=mpcplot.Style(use_tex=False), rows=1)
170 ax1 = axs[0]
171 ax1.set_ylabel(r"$\epsilon$ in kWh")
172 energy_flex_neg.plot(ax=ax1, label="neg")
173 energy_flex_pos.plot(ax=ax1, label="pos")
174 energy_flex_neg.plot(ax=ax1, label="neg", color=mpcplot.EBCColors.red)
175 energy_flex_pos.plot(ax=ax1, label="pos", color=mpcplot.EBCColors.blue)
177 ax1.legend()
179 x_ticks = np.arange(0, 3600 * 6 + 1, 3600)
180 x_tick_labels = [int(tick / 3600) for tick in x_ticks]
181 ax1.set_xticks(x_ticks)
182 ax1.set_xticklabels(x_tick_labels)
183 ax1.set_xlabel("Time in hours")
184 for ax in axs:
185 mpcplot.make_grid(ax)
186 ax.set_xlim(0, 3600 * 6)
188 plt.show()