Conditions | 9 |
Total Lines | 85 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import os |
||
185 | def plot_dispatch_invest(csv_path, mode="dark"): |
||
186 | """ |
||
187 | draws a demand timeseries next to an bar plot symbolizing the investment decisions |
||
188 | """ |
||
189 | |||
190 | df = pd.read_csv(csv_path) |
||
191 | |||
192 | if "Time" not in df.columns: |
||
193 | df["Time"] = pd.date_range( |
||
194 | start="2020-01-01", periods=len(df), freq="H" |
||
195 | ) |
||
196 | |||
197 | if mode.lower() == "dark": |
||
198 | palette = dark_palette |
||
199 | bg_color = "#121212" |
||
200 | text_color = "#FFFFFF" |
||
201 | else: |
||
202 | palette = light_palette |
||
203 | bg_color = "#FFFFFF" |
||
204 | text_color = "#000000" |
||
205 | |||
206 | # create two subplots |
||
207 | fig, axs = plt.subplots(1, 2, figsize=(16, 6)) |
||
208 | fig.patch.set_facecolor(bg_color) |
||
209 | |||
210 | ax1 = axs[0] |
||
211 | ax1.set_facecolor(bg_color) |
||
212 | for spine in ax1.spines.values(): |
||
213 | spine.set_color(text_color) |
||
214 | ax1.tick_params(colors=text_color, labelleft=False) |
||
215 | |||
216 | # draw dispatch timeseries |
||
217 | ax1.plot( |
||
218 | df["Time"], |
||
219 | df["demand_th"], |
||
220 | marker="o", |
||
221 | linestyle="-", |
||
222 | color=palette["color1"], |
||
223 | linewidth=2, |
||
224 | ) |
||
225 | ax1.set_title("Dispatch Time Series (kW)", color=text_color, fontsize=14) |
||
226 | ax1.set_xlabel("Time", color=text_color) |
||
227 | ax1.set_ylabel("kW", color=text_color) |
||
228 | |||
229 | avg_pv = df["pv"].mean() if "pv" in df.columns else 0 |
||
230 | avg_wind = df["wind"].mean() if "wind" in df.columns else 0 |
||
231 | avg_heatpump = 0.3 |
||
232 | avg_solarthermie = 0.09 |
||
233 | |||
234 | labels = ["PV", "Wind", "Heat Pump", "Solarthermie"] |
||
235 | values = [avg_pv, avg_wind, avg_heatpump, avg_solarthermie] |
||
236 | |||
237 | bar_colors = [ |
||
238 | palette["color3"], |
||
239 | palette["color4"], |
||
240 | palette["color2"], |
||
241 | palette["color5"], |
||
242 | ] |
||
243 | |||
244 | ax2 = axs[1] |
||
245 | ax2.set_facecolor(bg_color) |
||
246 | for spine in ax2.spines.values(): |
||
247 | spine.set_color(text_color) |
||
248 | ax2.tick_params(colors=text_color, labelleft=False) |
||
249 | |||
250 | bars = ax2.bar( |
||
251 | labels, values, color=bar_colors, edgecolor=text_color, linewidth=1.5 |
||
252 | ) |
||
253 | |||
254 | ax2.set_title("Investment Technology Sizes", color=text_color, fontsize=14) |
||
255 | ax2.set_ylabel("Average Value", color=text_color, fontsize=14) |
||
256 | |||
257 | fig.suptitle( |
||
258 | "Dispatch vs. Invest-Optimization", color=text_color, fontsize=16 |
||
259 | ) |
||
260 | |||
261 | plt.tight_layout(rect=[0, 0, 1, 0.95]) |
||
262 | |||
263 | # save plots |
||
264 | if mode == "dark": |
||
265 | |||
266 | plt.savefig("plot_dispatch_invest_dark.png") |
||
267 | if mode == "light": |
||
268 | |||
269 | plt.savefig("plot_dispatch_invest_light.png") |
||
270 | |||
292 |