-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_dispatch.py
More file actions
303 lines (271 loc) · 12.1 KB
/
plot_dispatch.py
File metadata and controls
303 lines (271 loc) · 12.1 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
# Plotting function for generation dispatch
# Interactive version (optional)
import pandas as pd
import numpy as np
def plot_dispatch(n, time="2024", days=None, regions=None,
show_imports=True, show_curtailment=True,
scenario_name=None, scenario_objective=None, interactive=False):
"""
Plot a generation dispatch stack by carrier for a PyPSA network, with optional
net imports/exports and a region‑filtered curtailment overlay.
Parameters
----------
n : pypsa.Network
The PyPSA network to plot.
time : str, default "2024"
Start of the time window (e.g. "2024", "2024-07", or "2024-07-15").
days : int, optional
Number of days from `time` to include in the plot.
regions : list of str, optional
Region bus names to filter by. If None, the entire network is included.
show_imports : bool, default True
Whether to include net imports/exports in the dispatch stack.
show_curtailment : bool, default True
Whether to calculate and plot VRE curtailment (Solar, Wind, Rooftop Solar).
scenario_name : str, optional
Scenario label to display below the title.
scenario_objective : str, optional
Objective description to display next to the legend.
interactive : bool, default False
Whether to create an interactive plot using Plotly instead of matplotlib.
Notes
-----
- All power values are converted to GW.
- Curtailment is plotted as a dashed black line if enabled.
- Demand (load) is plotted as a solid green line.
- Storage charging and net exports (negative values) are shown below zero.
"""
if interactive:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
else:
import matplotlib.pyplot as plt
# 1) REGION MASKS
if regions is not None:
gen_mask = n.generators.bus.isin(regions)
sto_mask = n.storage_units.bus.isin(regions) if not n.storage_units.empty else []
store_mask = n.stores.bus.isin(regions) if not n.stores.empty else []
region_buses = set(regions)
else:
gen_mask = pd.Series(True, index=n.generators.index)
sto_mask = pd.Series(True, index=n.storage_units.index) if not n.storage_units.empty else pd.Series(dtype=bool)
store_mask = pd.Series(True, index=n.stores.index) if not n.stores.empty else pd.Series(dtype=bool)
region_buses = set(n.buses.index)
# 2) AGGREGATE BY CARRIER (GW)
def _agg(df_t, df_stat, mask):
return (
df_t.loc[:, mask]
.T
.groupby(df_stat.loc[mask, 'carrier'])
.sum()
.T
.div(1e3)
)
p_by_carrier = _agg(n.generators_t.p, n.generators, gen_mask)
if not n.storage_units.empty:
p_by_carrier = pd.concat([p_by_carrier,
_agg(n.storage_units_t.p, n.storage_units, sto_mask)],
axis=1)
if not n.stores.empty:
p_by_carrier = pd.concat([p_by_carrier,
_agg(n.stores_t.p, n.stores, store_mask)],
axis=1)
# 3) TIME WINDOW
parts = time.split("-")
if len(parts) == 1:
start = pd.to_datetime(f"{parts[0]}-01-01")
elif len(parts) == 2:
start = pd.to_datetime(f"{parts[0]}-{parts[1]}-01")
else:
start = pd.to_datetime(time)
if days is not None:
end = start + pd.Timedelta(days=days) - pd.Timedelta(hours=1)
elif len(parts) == 1:
end = pd.to_datetime(f"{parts[0]}-12-31 23:00")
elif len(parts) == 2:
end = start + pd.offsets.MonthEnd(0) + pd.Timedelta(hours=23)
else:
end = start + pd.Timedelta(hours=23)
p_slice = p_by_carrier.loc[start:end].copy()
# drop carriers with zero activity
zero = p_slice.columns[p_slice.abs().sum() == 0]
p_slice.drop(columns=zero, inplace=True)
# 4) IMPORTS/EXPORTS
if show_imports:
ac = ( n.lines_t.p0.loc[start:end, n.lines.bus1.isin(region_buses) & ~n.lines.bus0.isin(region_buses)].sum(axis=1)
+ n.lines_t.p1.loc[start:end, n.lines.bus0.isin(region_buses) & ~n.lines.bus1.isin(region_buses)].sum(axis=1) )
dc = ( n.links_t.p0.loc[start:end, n.links.bus1.isin(region_buses) & ~n.links.bus0.isin(region_buses)].sum(axis=1)
+ n.links_t.p1.loc[start:end, n.links.bus0.isin(region_buses) & ~n.links.bus1.isin(region_buses)].sum(axis=1) )
p_slice['Imports/Exports'] = (ac + dc).div(1e3)
if 'Imports/Exports' not in n.carriers.index:
n.carriers.loc['Imports/Exports','color']='#7f7f7f'
# 5) LOAD SERIES
if regions:
load_cols = [c for c in n.loads[n.loads.bus.isin(regions)].index if c in n.loads_t.p_set]
load_series = n.loads_t.p_set[load_cols].sum(axis=1)
else:
load_series = n.loads_t.p_set.sum(axis=1)
load_series = load_series.loc[start:end].div(1e3)
# 6) VRE CURTAILMENT (GW) if requested
if show_curtailment:
vre = ['Solar','Wind', 'Rooftop Solar']
mask_vre = gen_mask & n.generators.carrier.isin(vre)
avail = (n.generators_t.p_max_pu.loc[start:end, mask_vre]
.multiply(n.generators.loc[mask_vre,'p_nom'], axis=1))
disp = n.generators_t.p.loc[start:end, mask_vre]
curtail = (avail.sub(disp, fill_value=0)
.clip(lower=0)
.sum(axis=1)
.div(1e3))
else:
curtail = None
# 7) PLOT
title_tail = f" for {', '.join(regions)}" if regions else ''
plot_title = f"Dispatch by Carrier: {start.date()} to {end.date()}{title_tail}"
if interactive:
# PLOTLY INTERACTIVE PLOT
fig = go.Figure()
fig.update_layout(
plot_bgcolor='#F0FFFF',
xaxis=dict(gridcolor='#DDDDDD'),
yaxis=dict(gridcolor='#DDDDDD') # Plot area background
)
# Prepare data for stacked area plot
positive_data = p_slice.where(p_slice > 0).fillna(0)
negative_data = p_slice.where(p_slice < 0).fillna(0)
# Add positive generation as stacked area
for i, col in enumerate(positive_data.columns):
if positive_data[col].sum() > 0:
color = n.carriers.loc[col, 'color']
# Only add points where value > 0.001
mask = positive_data[col].abs() > 0.001
if mask.any():
fig.add_trace(go.Scatter(
x=positive_data.index[mask],
y=positive_data[col][mask],
mode='lines',
fill='tonexty' if i > 0 else 'tozeroy',
line=dict(width=0, color=color),
fillcolor=color,
name=col,
stackgroup='positive',
hovertemplate='<b>%{fullData.name}</b><br>Power: %{y:.3f} GW<extra></extra>',
showlegend=True
))
# Add negative generation (storage charging, exports)
for col in negative_data.columns:
if negative_data[col].sum() < 0:
color = n.carriers.loc[col, 'color']
# Only add points where value < -0.001
mask = negative_data[col].abs() > 0.001
if mask.any():
fig.add_trace(go.Scatter(
x=negative_data.index[mask],
y=negative_data[col][mask],
mode='lines',
fill='tonexty',
line=dict(width=0, color=color),
fillcolor=color,
name=col,
stackgroup='negative',
hovertemplate='<b>%{fullData.name}</b><br>Power: %{y:.2f} GW<extra></extra>',
showlegend=True
))
# Add demand line (always show)
fig.add_trace(go.Scatter(
x=load_series.index,
y=load_series,
mode='lines',
line=dict(color='green', width=2),
name='Demand',
hovertemplate='<b>Demand</b><br>Power: %{y:.2f} GW<extra></extra>',
showlegend=True
))
# Add curtailment line if requested
if show_curtailment and curtail is not None:
fig.add_trace(go.Scatter(
x=curtail.index,
y=curtail,
mode='lines',
line=dict(color='black', width=2, dash='dash'),
name='Curtailment',
hovertemplate='<b>Curtailment</b><br>Power: %{y:.2f} GW<extra></extra>',
showlegend=True
))
# Update layout
fig.update_layout(
title=plot_title,
xaxis_title='Time',
yaxis_title='GW',
hovermode='x unified',
hoverlabel=dict(
bgcolor="white",
bordercolor="black",
font_size=12,
),
legend=dict(
x=1.02,
y=1,
bgcolor='rgba(255,255,255,0.8)',
bordercolor='rgba(0,0,0,0.2)',
borderwidth=1
),
width=800,
height=550
)
# Add scenario annotations
annotations = []
if scenario_name:
annotations.append(
dict(
x=1.02, y=-0.05,
xref='paper', yref='paper',
text=f"Scenario: {scenario_name}",
showarrow=False,
font=dict(size=10, color='gray'),
xanchor='center',
yanchor='top'
)
)
if annotations:
fig.update_layout(annotations=annotations)
# fig.show()
return fig
else:
# MATPLOTLIB STATIC PLOT
fig, ax = plt.subplots(figsize=(8.4, 6.5)) #12,6.5
cols = p_slice.columns.map(lambda c: n.carriers.loc[c,'color'])
p_slice.where(p_slice>0).plot.area(ax=ax,linewidth=0,color=cols)
neg = p_slice.where(p_slice<0).dropna(how='all',axis=1)
if not neg.empty:
neg_cols=[n.carriers.loc[c,'color'] for c in neg.columns]
neg.plot.area(ax=ax,linewidth=0,color=neg_cols)
load_series.plot(ax=ax,color='g',linewidth=1.5,label='Demand')
if show_curtailment and curtail is not None:
curtail.plot(ax=ax,color='k',linestyle='--',linewidth=1.2,label='Curtailment')
# limits & legend
up = max(p_slice.where(p_slice>0).sum(axis=1).max(),
load_series.max(),
curtail.max() if curtail is not None else 0)
dn = min(p_slice.where(p_slice<0).sum(axis=1).min(), load_series.min())
ax.set_ylim(dn if not np.isclose(up,dn) else dn-0.1, up)
# fig.patch.set_facecolor('#F0FFFF')
ax.set_facecolor('#F0FFFF')
h,l = ax.get_legend_handles_labels()
seen={} ; fh,fl=[],[]
for hh,ll in zip(h,l):
if ll not in seen: fh.append(hh);fl.append(ll);seen[ll]=True
ax.legend(fh,fl,loc=(1.02,0.67), fontsize=9)
# scenario text
if scenario_objective:
ax.text(1.02,0.01,f"Objective:\n{scenario_objective}",transform=ax.transAxes,
fontsize=8,va='bottom',ha='left',bbox=dict(facecolor='white',alpha=0.7,edgecolor='none'))
if scenario_name:
ax.text(1.02,-0.05,f"Scenario: {scenario_name}",transform=ax.transAxes,
fontsize=9,color='gray',ha='center',va='top')
ax.set_ylabel('GW')
ax.set_title(plot_title)
plt.tight_layout()
# plt.show()
return fig