Conditions | 1 |
Total Lines | 77 |
Code Lines | 43 |
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 | """ |
||
33 | def plot_installedcapacity(carrier, scenario="eGon2035"): |
||
34 | """ |
||
35 | Plots color maps according to the capacity of different generators |
||
36 | of the two existing scenarios (eGon2035 and eGon100RE) |
||
37 | |||
38 | |||
39 | Parameters |
||
40 | ---------- |
||
41 | carrier : generators |
||
42 | The list of generators: biomass, central_biomass_CHP, central_biomass_CHP_heat, |
||
43 | industrial_biomass_CHP, solar, solar_rooftop, wind_offshore, wind_onshore. |
||
44 | |||
45 | scenario: eGon2035, eGon100RE |
||
46 | |||
47 | Returns |
||
48 | ---------- |
||
49 | """ |
||
50 | |||
51 | # This function must be called while in the folder |
||
52 | # that contains the file egon-data.configuration.yaml. |
||
53 | con = db.engine() |
||
54 | # imports buses of Germany |
||
55 | SQLBus = ( |
||
56 | "SELECT bus_id, country FROM grid.egon_etrago_bus WHERE country='DE'" |
||
57 | ) |
||
58 | busDE = pd.read_sql(SQLBus, con) |
||
59 | busDE = busDE.rename({"bus_id": "bus"}, axis=1) |
||
60 | # Imports grid districs |
||
61 | sql = "SELECT bus_id, geom FROM grid.egon_mv_grid_district" |
||
62 | distr = gpd.GeoDataFrame.from_postgis(sql, con) |
||
63 | distr = distr.rename({"bus_id": "bus"}, axis=1) |
||
64 | distr = distr.set_index("bus") |
||
65 | # merges grid districts with buses |
||
66 | distr = pd.merge(busDE, distr, on="bus") |
||
67 | # Imports generator |
||
68 | sqlCarrier = "SELECT carrier, p_nom, bus FROM grid.egon_etrago_generator" |
||
69 | sqlCarrier = "SELECT * FROM grid.egon_etrago_generator" |
||
70 | Carriers = pd.read_sql(sqlCarrier, con) |
||
71 | Carriers = Carriers.loc[Carriers["scn_name"] == scenario] |
||
72 | Carriers = Carriers.set_index("bus") |
||
73 | |||
74 | CarrierGen = Carriers.loc[Carriers["carrier"] == carrier] |
||
75 | # merges districts with generators |
||
76 | Merge = pd.merge(CarrierGen, distr, on="bus", how="outer") |
||
77 | |||
78 | Merge.loc[Merge["carrier"] != carrier, "p_nom"] = 0 |
||
79 | Merge.loc[Merge["country"] != "DE", "p_nom"] = 0 |
||
80 | |||
81 | gdf = gpd.GeoDataFrame(Merge, geometry="geom") |
||
82 | pnom = gdf["p_nom"] # |
||
83 | # 0.95 quantile is used to filter values that are too high and make noise in the plots. |
||
84 | max_pnom = pnom.quantile(0.95) |
||
85 | gdf = gdf.to_crs(epsg=3857) |
||
86 | |||
87 | fig, ax = plt.subplots(1, 1) |
||
88 | |||
89 | ax.set_axis_off() |
||
90 | plt.title(f" {carrier} installed capacity in MW , {scenario}") |
||
91 | cmap = mpl.cm.coolwarm |
||
92 | |||
93 | norm = mpl.colors.Normalize(vmin=0, vmax=max_pnom) |
||
94 | gdf.plot( |
||
95 | column="p_nom", |
||
96 | ax=ax, |
||
97 | legend=False, |
||
98 | legend_kwds={"label": "p_nom(MW)", "orientation": "vertical"}, |
||
99 | cmap=cmap, |
||
100 | norm=norm, |
||
101 | edgecolor="black", |
||
102 | linewidth=0.1, |
||
103 | zorder=2, |
||
104 | ) |
||
105 | scatter = ax.collections[0] |
||
106 | cbar = plt.colorbar(scatter, ax=ax, extend="max") |
||
107 | cbar.set_label("p_nom(MW)", rotation=90) |
||
108 | |||
109 | return |
||
110 |