-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_plotting.py
More file actions
414 lines (373 loc) · 15.6 KB
/
Copy pathbasic_plotting.py
File metadata and controls
414 lines (373 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
'''
basic plotting
'''
import matplotlib.pyplot as plt
import pypsa
import cartopy.crs as ccrs
import pandas as pd
import polars as pl
import geopandas as gpd
from shapely.geometry import Polygon
def plot_grid(grid,
area,
features):
"""
plots the pypsa grid along with OSM data and features.
Args:
grid (pypsa.Network): The power system network containing buses and generators.
area (gpd.GeoDataFrame): GeoDataFrame containing OSM area data with geometry.
features (gpd.GeoDataFrame): GeoDataFrame containing OSM features with geometry.
"""
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={'projection': ccrs.PlateCarree()})
# pypsa grid
grid.plot(ax=ax,
bus_sizes=1 / 2e9,
margin=1)
# OSM data
# ox.plot_graph(area, ax=ax, show=False, close=False)
area.plot(ax=ax, facecolor="lightgrey", edgecolor="black", alpha=0.5)
features.plot(ax=ax, facecolor="khaki", edgecolor="black", alpha=0.1)
plt.legend()
plt.title('Grid Overview')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()
def plot_step1(grid,
bus_sizes=1 / 2e9,
figsize=(10,10),
plot_trafos=True,
bool_legend=True,
legend_loc=None,
bool_gridlines=True,
bool_gridlinelabels=False,
title:str=None,
legend_fontsize: int = 12,
legend_markerscale: float = 1.5,
gridlabel_size: int = 12):
"""
plots the pypsa grid.
Args:
grid (pypsa.Network): The power system network containing buses and generators.
Returns:
fig, ax: Matplotlib figure and axis objects.
"""
fig, ax = plt.subplots(figsize=figsize, subplot_kw={'projection': ccrs.PlateCarree()})
# pypsa grid
grid.plot(ax=ax, bus_sizes=bus_sizes, margin=0)
if plot_trafos:
# Marking transformer buses
tra_buses = grid.transformers['bus1'].unique()
tra_coords = grid.buses.loc[tra_buses][['x', 'y']]
ax.scatter(
tra_coords['x'],
tra_coords['y'],
color='red',
s=10,
label='Transformers',
zorder=5,
transform=ccrs.PlateCarree()
)
if bool_legend:
handles = [
plt.Line2D([0], [0], marker='o', color='w', markerfacecolor='cadetblue', markersize=10),
plt.Line2D([0, 1], [0, 0], color='rosybrown', lw=2)
]
labels = ['Nodes', 'Lines']
if plot_trafos:
handles.append(plt.Line2D([0], [0], marker='o', color='w', markerfacecolor='red', markersize=10))
labels.append('Transformers')
if legend_loc is None:
legend_loc = 'upper right'
plt.legend(handles=handles, labels=labels, loc=legend_loc)
ax.legend(
handles,
labels,
loc=legend_loc,
fontsize=legend_fontsize,
markerscale=legend_markerscale,
title="Legend",
title_fontsize=legend_fontsize + 1,
)
if title is not None:
plt.title(title)
# Customize gridlines and labels
if not bool_gridlinelabels:
ax.tick_params(bottom=False, left=False)
if bool_gridlines:
# Add cartopy gridlines that draw latitude/longitude tick labels and set axis labels with padding
gl = ax.gridlines(draw_labels=bool_gridlinelabels, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')
# disable labels on top/right to avoid duplication
try:
gl.top_labels = False
gl.right_labels = False
except Exception:
# some cartopy versions use different attributes; ignore if not available
pass
if bool_gridlinelabels:
gl.xlabel_style = {"size": gridlabel_size}
gl.ylabel_style = {"size": gridlabel_size}
plt.tight_layout()
return fig, ax
def plot_step2_only_osm(grid,
features,
bus_sizes=1 / 2e9,
figsize=(10,10),
plot_trafos=True,
bool_legend=True,
legend_loc=None,
bool_gridlines=True,
bool_gridlinelabels=False,
title:str=None):
"""
plots the pypsa grid along with OSM data features.
Args:
grid (pypsa.Network): The power system network containing buses and generators.
features (gpd.GeoDataFrame): GeoDataFrame containing OSM features with geometry.
Returns:
fig, ax: Matplotlib figure and axis objects.
"""
fig, ax = plot_step1(grid, bus_sizes, figsize, plot_trafos, bool_legend,
legend_loc, bool_gridlines, bool_gridlinelabels)
# OSM data
features_polygons = features[features.geometry.type.isin(["Polygon", "MultiPolygon"])]
features_polygons.plot(ax=ax, facecolor="khaki", edgecolor="black", alpha=0.2, transform=ccrs.PlateCarree())
if title is not None:
plt.title(title)
return fig, ax
def plot_step2(grid: pypsa.Network,
features: gpd.GeoDataFrame,
buses: pd.DataFrame,
zensus_path: str,
zensus_feature: str,
zensus_feature_nicename: str,
title:str=None,
bus_sizes=1 / 2e9,
figsize=(10,10),
plot_trafos=True,
bool_legend=True,
legend_loc='lower right',
bool_gridlines=True,
bool_gridlinelabels=False,
cbar_labelsize: int = 20,
cbar_ticksize: int = 15)->None:
'''
plots the pypsa grid along with OSM data and Census features.
Args:
grid (pypsa.Network): The power system grid containing buses and generators.
features (gpd.GeoDataFrame): GeoDataFrame containing OSM features with geometry.
buses (pd.DataFrame): DataFrame containing bus information with 'GITTER_ID_100m'.
zensus_path (str): Path to the census data CSV file.
zensus_feature (str): The specific feature from the census data to be visualized.
zensus_feature_nicename (str): A user-friendly name for the census feature to be used in the plot.
Returns:
fig, ax: Matplotlib figure and axis objects.
'''
if zensus_feature is None:
warn.warning("No zensus feature provided, defaulting to 'durchschnMieteQM'")
zensus_feature = "durchschnMieteQM"
# Loading Census Data and preparing Polygons
ids = buses['GITTER_ID_100m'].copy()
zensus = (pl.scan_csv(zensus_path, separator=";")
.filter(pl.col("GITTER_ID_100m").is_in(ids))
.select("GITTER_ID_100m",
"x_mp_100m",
"y_mp_100m",
zensus_feature,
).collect()
)
zensus = zensus.to_pandas()
zensus[zensus_feature] = (zensus[zensus_feature].astype(str).str.replace(",", ".", regex=False).replace(r"[^\d\.-]", "",regex=True).replace("", "0").astype(float))
# Build Polygons from Census Paoints
def build_cell(row, half=50):
x = row["x_mp_100m"]
y = row["y_mp_100m"]
return Polygon([
(x - half, y - half),
(x - half, y + half),
(x + half, y + half),
(x + half, y - half)
])
# Creating GeoDataFrame for Census Data
zensus["geometry"] = zensus.apply(build_cell, axis=1)
zensus_gdf = gpd.GeoDataFrame(zensus, geometry="geometry", crs="EPSG:3035")
# to EPSG:4326
zensus_gdf = zensus_gdf.to_crs("EPSG:4326")
zensus_voronoi_gdf = zensus_gdf[["geometry", zensus_feature]].copy()
# Plotting the network
fig, ax = plot_step2_only_osm(grid, features, bus_sizes, figsize, plot_trafos, bool_legend,
legend_loc, bool_gridlines, bool_gridlinelabels)
# Plot zensus['geometry']
zensus_gdf["geometry"].plot(ax=ax, facecolor="none", edgecolor="black")
# Census as heatmap overlay
zensus_voronoi_gdf.plot(
column=zensus_feature,
cmap="viridis",
legend=False,
ax=ax,
alpha=0.7,
edgecolor="black",
linewidth=0.2,
transform=ccrs.PlateCarree()
)
# Setting the legend
# Creating the colorbar
sm = plt.cm.ScalarMappable(cmap="viridis",
norm=plt.Normalize(
vmin=zensus_voronoi_gdf[zensus_feature].min(),
vmax=zensus_voronoi_gdf[zensus_feature].max()
))
sm._A = []
cbar = plt.colorbar(sm, ax=ax, orientation="vertical", shrink=0.7)
cbar.set_label(zensus_feature_nicename, fontsize=cbar_labelsize)
cbar.ax.tick_params(labelsize=cbar_ticksize)
if title is not None:
plt.title(title)
plt.tight_layout()
return fig, ax
def plot_step3(grid: pypsa.Network,
features: gpd.GeoDataFrame,
buses: pd.DataFrame,
zensus_path: str,
zensus_feature: str,
zensus_feature_nicename: str,
title:str=None,
bus_sizes=1 / 2e9,
figsize=(10,10),
bool_legend=True,
legend_loc='lower right',
plot_trafos=True,
bool_gridlines=True,
bool_gridlinelabels=False,
axis_labelsize: int = 14,
tick_labelsize: int = 12,
title_size: int = 16,
legend_fontsize: int = 12,
legend_markerscale: float = 1.5,
cbar_labelsize: int = 20,
cbar_ticksize: int = 15,
xaxis_label_rotation: int = 0,
xtick_rotation: int = 0)->None:
'''
plots the pypsa grid along with OSM data and Census features and assigned generators.
Args:
grid (pypsa.Network): The power system grid containing buses and generators.
features (gpd.GeoDataFrame): GeoDataFrame containing OSM features with geometry.
buses (pd.DataFrame): DataFrame containing bus information with 'GITTER_ID_100m'.
zensus_path (str): Path to the census data CSV file.
zensus_feature (str): The specific feature from the census data to be visualized.
zensus_feature_nicename (str): A user-friendly name for the census feature to be used in the plot.
Returns:
fig, ax: Matplotlib figure and axis objects.
'''
# Filtering generators and storages
gen_buses = {
'solar': set(grid.generators[grid.generators.index.to_series().str.contains('_solar')]['bus']),
'HP': set(grid.generators[grid.generators.index.to_series().str.contains('_HP')]['bus'])
}
storage_buses = {
'E_Auto': set(grid.buses[grid.buses.index.isin(grid.links['bus0'])].index)
}
# Defining categories: bus sets, color, label
gen_categories = [
(gen_buses['solar'], 'yellow', 'PV'),
(storage_buses['E_Auto'], 'green', 'EV'),
(gen_buses['HP'], 'blue', 'HP'),
(gen_buses['HP'] & gen_buses['solar'], 'purple', 'HP & PV'),
(gen_buses['HP'] & storage_buses['E_Auto'], 'pink', 'HP & EV'),
(gen_buses['solar'] & storage_buses['E_Auto'], 'violet', 'PV & EV'),
(gen_buses['HP'] & gen_buses['solar'] & storage_buses['E_Auto'], 'orange', 'HP, PV & EV')
]
# Conventional Generators
gen_buses['conventional'] = set(grid.generators[grid.generators.index.to_series().str.contains('_conventional')]['bus'])
# Defining bus sizes
bus_size_set = pd.Series(index=grid.buses.index, dtype=object)
# Setting all buses with carrier "external_supercharger" to 0
for bus in grid.buses.index:
if "external_supercharger" in bus:
bus_size_set.at[bus] = 0
else:
bus_size_set.at[bus] = 1 / 2e9 # Standardgröße
# Plotting the network
fig, ax = plot_step2(grid, features, buses, zensus_path, zensus_feature=zensus_feature,
zensus_feature_nicename=zensus_feature_nicename, bus_sizes=bus_size_set,
figsize=figsize, plot_trafos=plot_trafos, bool_legend=False,
bool_gridlines=bool_gridlines, bool_gridlinelabels=bool_gridlinelabels)
# Plotting different generator types
for buses, color, label in gen_categories:
if buses:
coords = grid.buses.loc[list(buses), ['x', 'y']]
ax.scatter(
coords['x'], coords['y'],
color=color,
s=20,
label=label,
zorder=5,
transform=ccrs.PlateCarree()
)
# Plotting conventional generators in brown
if gen_buses['conventional']:
coords = grid.buses.loc[list(gen_buses['conventional']), ['x', 'y']]
ax.scatter(
coords['x'], coords['y'],
color='brown',
s=20,
label='Conventional Generatoren',
zorder=5,
transform=ccrs.PlateCarree()
)
if title is not None:
plt.title(title)
if bool_legend:
handles, labels = ax.get_legend_handles_labels()
if legend_loc is None:
legend_loc = 'upper right'
plt.legend(handles=handles, labels=labels, loc=legend_loc, fontsize=legend_fontsize,)
plt.tight_layout()
return fig, ax
def plot_step4(grid,
bus = None,
figsize=(10,10),
begin:int=0,
gridlines=True,
title:str=None,
ylabel='Power (kW)',
xlabel='Time'):
"""
Plots the load, solar generator and heat pump time series for a specified bus over one week.
If no bus is specified, the first bus with more than one generator is selected.
Args:
grid (pypsa.Network): The power system network containing buses and generators.
bus (str, optional): The bus to plot. If None, the first bus with more than one generator is selected.
Returns:
fig, ax: Matplotlib figure and axis objects.
"""
if bus is None:
# Grouping generators by buses
grouped = grid.generators.groupby('bus')
gen_dict = {bus: grouped.get_group(bus) for bus in grouped.groups}
# keys with multiple generators
keys_multiple_gens = [key for key, value in gen_dict.items() if len(value) > 1]
# select the first one
bus = keys_multiple_gens[0]
# for one week
end = begin + 24*7
solar = f'{bus}_solar'
hp = f'{bus}_HP'
# plot of Load
fig, ax = plt.subplots(figsize=figsize)
(grid.loads_t.p_set[f'{bus}_load_1'].iloc[begin:end]*1e6).plot(ax=ax, label='Load', color='green')
# Plot of only one week
if solar in grid.generators.index:
(grid.generators_t.p_max_pu[solar][begin:end]*grid.generators.at[solar, 'p_nom']*1e6).plot(ax=ax, label='Solar Generator', linestyle='-', color='orange')
if hp in grid.generators.index:
(grid.generators_t.p_max_pu[hp][begin:end]*grid.generators.at[hp, 'p_nom']*1e6).plot(ax=ax, label='Heat Pump', linestyle='-', color='blue')
if gridlines:
ax.grid()
if title is not None:
plt.title(title)
plt.grid()
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.legend()
plt.tight_layout()
return fig, ax