Conditions | 2 |
Total Lines | 79 |
Code Lines | 37 |
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 | #!/usr/bin/env python3 |
||
107 | def plot_generation( |
||
108 | carrier,scenario, osm = False |
||
109 | ): |
||
110 | """ |
||
111 | Plots color maps according to the capacity of different generators |
||
112 | of the two existing scenarios (eGon2035 and eGon100RE) |
||
113 | |||
114 | |||
115 | Parameters |
||
116 | ---------- |
||
117 | carrier : generators |
||
118 | The list of generators: biomass, central_biomass_CHP, central_biomass_CHP_heat, |
||
119 | industrial_biomass_CHP, solar, solar_rooftop, wind_offshore, wind_onshore. |
||
120 | |||
121 | scenario: eGon2035, eGon100RE |
||
122 | |||
123 | |||
124 | """ |
||
125 | |||
126 | |||
127 | con = db.engine() |
||
128 | #imports buses of Germany |
||
129 | SQLBus = "SELECT bus_id, country FROM grid.egon_etrago_bus WHERE country='DE'" |
||
130 | busDE = pd.read_sql(SQLBus,con) |
||
131 | busDE = busDE.rename({'bus_id': 'bus'},axis=1) |
||
132 | #Imports grid districs |
||
133 | sql = "SELECT bus_id, geom FROM grid.egon_mv_grid_district" |
||
134 | distr = gpd.GeoDataFrame.from_postgis(sql, con) |
||
135 | distr = distr.rename({'bus_id': 'bus'},axis=1) |
||
136 | distr = distr.set_index("bus") |
||
137 | #merges grid districts with buses |
||
138 | distr = pd.merge(busDE, distr, on='bus') |
||
139 | #Imports generator |
||
140 | sqlCarrier = "SELECT carrier, p_nom, bus FROM grid.egon_etrago_generator" |
||
141 | sqlCarrier = "SELECT * FROM grid.egon_etrago_generator" |
||
142 | Carriers = pd.read_sql(sqlCarrier,con) |
||
143 | Carriers = Carriers.loc[Carriers['scn_name'] == scenario] |
||
144 | Carriers = Carriers.set_index("bus") |
||
145 | |||
146 | |||
147 | CarrierGen = Carriers.loc[Carriers['carrier'] == carrier] |
||
148 | #merges districts with generators |
||
149 | Merge = pd.merge(CarrierGen, distr, on ='bus', how="outer") |
||
150 | |||
151 | |||
152 | Merge.loc[Merge ['carrier'] != carrier, "p_nom" ] = 0 |
||
153 | Merge.loc[Merge ['country'] != "DE", "p_nom" ] = 0 |
||
154 | |||
155 | gdf = gpd.GeoDataFrame(Merge , geometry='geom') |
||
156 | pnom=gdf['p_nom'] # |
||
157 | #0.95 quantile is used to filter values that are too high and make noise in the plots. |
||
158 | max_pnom=pnom.quantile(0.95) |
||
159 | gdf = gdf.to_crs(epsg=3857) |
||
160 | |||
161 | |||
162 | # Plot osm map in background |
||
163 | if osm != False: |
||
164 | #if network.srid == 4326: |
||
165 | #set_epsg_network(network) |
||
166 | fig, ax = plot_osm(osm['x'], osm['y'], osm['zoom']) |
||
167 | |||
168 | else: |
||
169 | fig, ax = plt.subplots(1, 1) |
||
170 | |||
171 | |||
172 | |||
173 | ax.set_axis_off(); |
||
174 | plt.title(f" {carrier} installed capacity in MW , {scenario}") |
||
175 | cmap = mpl.cm.coolwarm |
||
176 | |||
177 | |||
178 | norm = mpl.colors.Normalize(vmin=0, vmax=max_pnom) |
||
179 | gdf.plot(column='p_nom', ax=ax, legend=False, legend_kwds={'label': "p_nom(MW)", |
||
180 | |||
181 | 'orientation': "vertical"}, cmap=cmap, norm=norm, edgecolor='black', linewidth=0.1,zorder=2) |
||
182 | scatter = ax.collections[0] |
||
183 | cbar=plt.colorbar(scatter, ax=ax, extend='max') |
||
184 | cbar.set_label('p_nom(MW)', rotation=90) |
||
185 | return 0 |
||
186 | |||
191 |