generated from rl-institut/rli_template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from rl-institut/features/plot-scalar-resources
Features/plot scalar resources
- Loading branch information
Showing
10 changed files
with
303 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,4 @@ Plotting | |
:maxdepth: 1 | ||
:glob: | ||
|
||
plotting/* | ||
plotting/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. _plot_scalar_resources_label: | ||
|
||
plot_scalar_resources | ||
===================== | ||
|
||
.. automodule:: plot_conv_pp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Battery in: Batterie in | ||
Battery out: Batterie out | ||
Battery: Batterie | ||
Biomass GT: Biomass GT | ||
Biomass ST: Biomass ST | ||
Biomass: Biomasse | ||
CH4 Backpressure CHP: CH4 Backpressure CHP | ||
CH4 Boiler: CH4 Boiler | ||
CH4 Excess: CH4 Excess | ||
CH4 Extraction CHP: CH4 Extraction CHP | ||
CH4 GT: CH4 GT | ||
CH4: CH4 | ||
Curtailment: Curtailment | ||
El. demand: El. demand | ||
El. in: Batterie in | ||
Electrolyzer: Electrolyzer | ||
H2 GT: H2 GT | ||
Hard coal: Steinkohle | ||
Heat demand: Heat demand | ||
Heat Excess: Heat Excess | ||
Heat Storage: Heat Storage | ||
Hydro reservoir: Reservoir | ||
Hydro ROR: Hydro ROR | ||
Lignite: Braunkohle | ||
Nuclear ST: Nuclear ST | ||
Oil: Öl | ||
Other: Andere | ||
PV: PV | ||
res. PtH: res. PtH | ||
Shortage: Shortage | ||
Transmission: Transmission | ||
Wind off: Wind off | ||
Wind on: Wind on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
r""" | ||
Inputs | ||
------- | ||
resources : str | ||
path of input file with data of prepared conventional power plant data as .csv | ||
target : str | ||
path of output file with plot | ||
Outputs | ||
--------- | ||
matplotlib.figure.Figure | ||
plotted conventional power plant data | ||
Description | ||
------------- | ||
The script takes prepared data of conventional power plants from OPSD, prepares the | ||
data further and plots the data. | ||
""" | ||
import os | ||
import sys | ||
|
||
import matplotlib.pyplot as plt | ||
import oemoflex.tools.plots as plots | ||
import pandas as pd | ||
|
||
import oemof_b3.tools.data_processing as dp | ||
from oemof_b3 import colors_odict, labels_to_german | ||
|
||
# User input | ||
# converting from MW to W | ||
conv_number = 1e6 | ||
german_translation = True | ||
unit = "W" | ||
|
||
|
||
# GENERAL | ||
c_to_cl = { | ||
"biomass": "Biomass", | ||
"ch4": "CH4", | ||
"hard coal": "Hard coal", | ||
"oil": "Oil", | ||
"lignite": "Lignite", | ||
"other": "Other", | ||
} | ||
|
||
cl_to_gcl = labels_to_german | ||
|
||
c_to_gcl = {carrier: cl_to_gcl[cl] for carrier, cl in c_to_cl.items()} | ||
|
||
colors_odict_german = { | ||
cl_to_gcl[cl]: color for cl, color in colors_odict.items() if cl in cl_to_gcl | ||
} | ||
|
||
|
||
def prepare_conv_pp_scalars(df_conv_pp_scalars, var_name, conv_number, label_mapping): | ||
r""" | ||
This function converts scalar data in oeomof-b3 format to a format that can be passed to | ||
`plot_grouped_bar` . | ||
It aggregates power plants with the same carrier. | ||
Parameters | ||
---------- | ||
df_conv_pp_scalars: pd.DataFrame | ||
contains all conventional power plants in Berlin and Brandenburg | ||
var_name: string | ||
indicates the var_name which shall be plotted | ||
conv_number: int | ||
converts the value to SI unit, i.e. W | ||
Returns | ||
------- | ||
df_pivot : pandas.DataFrame | ||
Unique unit | ||
color_dict: dict | ||
contains colors for different carriers for plotting | ||
""" | ||
# select var_name to be plotted | ||
selected_df = df_conv_pp_scalars[df_conv_pp_scalars["var_name"] == var_name].copy() | ||
|
||
assert not selected_df.empty, f"Variable name '{var_name}' not found in data." | ||
|
||
# CONV_PP specific | ||
selected_df.loc[(selected_df["region"] != "Berlin"), "region"] = "Brandenburg" | ||
|
||
# aggregate carriers in regions | ||
selected_df_agg = dp.aggregate_scalars( | ||
df=selected_df, columns_to_aggregate=["tech"] | ||
) | ||
|
||
# convert to SI unit: | ||
if conv_number: | ||
selected_df_agg.loc[:, "var_value"] *= conv_number | ||
|
||
selected_df_agg["carrier"].replace(label_mapping, inplace=True) | ||
|
||
# apply pivot table | ||
df_pivot = pd.pivot_table( | ||
selected_df_agg, | ||
index=["region"], | ||
columns="carrier", | ||
values="var_value", | ||
) | ||
|
||
return df_pivot | ||
|
||
|
||
def plot_grouped_bar(ax, df, color_dict, unit, stacked=False): | ||
r""" | ||
This function plots scalar data as grouped bar plot. The index of the DataFrame | ||
will be interpreted as groups (e.g. regions), the columns as different categories (e.g. energy | ||
carriers) within the groups which will be plotted in different colors. | ||
Parameters | ||
---------- | ||
ax: matplotlib Axes object | ||
Axes to draw the plot. | ||
df: pd.DataFrame | ||
DataFrame with an index defining the groups and columns defining the bars of different color | ||
with in the group. | ||
color_dict: dict | ||
Dictionary defining colors of the categories | ||
unit: str | ||
Unit of the variables | ||
stacked : boolean | ||
Stack bars of a group. False by default. | ||
""" | ||
alpha = 0.3 | ||
fontsize = 14 | ||
plt.rcParams.update({"font.size": fontsize}) | ||
|
||
# apply EngFormatter if power is plotted | ||
ax = plots._eng_format(ax, unit) | ||
|
||
df.plot.bar( | ||
ax=ax, | ||
color=[color_dict[key] for key in df.columns], | ||
width=0.8, | ||
zorder=2, | ||
stacked=stacked, | ||
rot=0, | ||
) | ||
|
||
for spine in ["top", "left", "right"]: | ||
ax.spines[spine].set_visible(False) | ||
ax.spines["bottom"].set_alpha(alpha) | ||
ax.tick_params(axis="both", length=0, pad=7) | ||
|
||
ax.grid(axis="y", zorder=1, color="black", alpha=alpha) | ||
ax.set_xlabel(xlabel=None) | ||
ax.legend() | ||
plt.legend(title=None, frameon=True, framealpha=1) | ||
|
||
plt.tight_layout() | ||
|
||
return ax | ||
|
||
|
||
if __name__ == "__main__": | ||
resources = sys.argv[1] | ||
|
||
var_name = sys.argv[2] | ||
|
||
target = sys.argv[3] | ||
|
||
target_dir = os.path.dirname(target) | ||
|
||
# create the directory plotted where all plots are saved | ||
if not os.path.exists(target_dir): | ||
os.makedirs(target_dir) | ||
|
||
# Load scalar data | ||
df_conv_pp_scalars = dp.load_b3_scalars(resources) | ||
|
||
if german_translation: | ||
label_mapping = c_to_gcl | ||
color_dict = colors_odict_german | ||
else: | ||
label_mapping = c_to_cl | ||
color_dict = colors_odict | ||
|
||
df_pivot = prepare_conv_pp_scalars( | ||
df_conv_pp_scalars=df_conv_pp_scalars, | ||
var_name=var_name, | ||
conv_number=conv_number, | ||
label_mapping=label_mapping, | ||
) | ||
|
||
fig, ax = plt.subplots(figsize=(12, 6)) | ||
|
||
plot_grouped_bar(ax, df_pivot, color_dict, unit) | ||
|
||
plt.savefig(target, bbox_inches="tight") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.