Skip to content

Commit e7fe2ae

Browse files
committed
Improve transmission plots
1 parent 30f12f9 commit e7fe2ae

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

switch_model/tools/graph/main.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import numpy as np
1616
import pandas as pd
1717
from PIL import Image
18-
from matplotlib import pyplot as plt
18+
from matplotlib import pyplot as plt, colors
1919
import seaborn as sns
2020
import matplotlib
2121
import plotnine
@@ -124,7 +124,6 @@ def __init__(self, graph_tools):
124124
self._tools = graph_tools
125125
self._wecc_states = None
126126
self._center_points = None
127-
self._figure_size = (11, 12)
128127
self._shapely = None
129128
self._geopandas = None
130129

@@ -216,7 +215,7 @@ def graph_pie_chart(self, df, max_size=2500):
216215
current_max_size = df.groupby("gen_load_zone")["value"].sum().max()
217216
df["value"] *= max_size / current_max_size
218217

219-
ax = self._tools.get_axes(size=self._figure_size)
218+
ax = self._tools.get_axes()
220219
self._plot_states(ax)
221220
df = df.merge(center_points, on="gen_load_zone")
222221

@@ -232,15 +231,41 @@ def graph_pie_chart(self, df, max_size=2500):
232231
ratios = (group_sum / total_size).values
233232
GraphMapTools._pie_plot(x, y, ratios, tech_color, ax=ax, size=total_size)
234233

235-
def graph_transmission(self, df):
234+
def graph_points(self, df, ax=None):
235+
"""
236+
Graphs a point in each load zone based on a dataframe with two columns
237+
- gen_load_zone
238+
- value
239+
"""
240+
_, center_points = self._load_maps()
241+
242+
df = df.merge(center_points, on="gen_load_zone")
243+
# Cast to GeoDataFrame
244+
df = self._geopandas.GeoDataFrame(
245+
df[["geometry", "value"]], geometry="geometry"
246+
)
247+
248+
if ax is None:
249+
ax = self._tools.get_axes()
250+
self._plot_states(ax)
251+
df.plot(
252+
ax=ax,
253+
column="value",
254+
legend=True,
255+
cmap="coolwarm",
256+
markersize=30,
257+
norm=colors.CenteredNorm(),
258+
)
259+
260+
def graph_transmission(self, df, cutoff):
236261
"""
237262
Graphs the data frame a dataframe onto a map.
238263
The dataframe should have 4 columns:
239264
- from: the load zone where we're starting from
240265
- to: the load zone where we're going to
241266
- value: the value to plot
242267
"""
243-
ax = self._tools.get_axes(size=self._figure_size)
268+
ax = self._tools.get_axes()
244269
_, center_points = self._load_maps()
245270

246271
# Merge duplicate rows if table was unidirectional
@@ -265,12 +290,21 @@ def make_line(r):
265290
return LineString([r["from_geometry"], r["to_geometry"]])
266291

267292
df["geometry"] = df.apply(make_line, axis=1)
293+
# Cast to GeoDataFrame
268294
df = self._geopandas.GeoDataFrame(
269295
df[["geometry", "value"]], geometry="geometry"
270296
)
271297

272298
self._plot_states(ax)
273-
df.plot(ax=ax, column="value", legend=True, cmap="Reds")
299+
df.plot(
300+
ax=ax,
301+
column="value",
302+
legend=True,
303+
cmap="Greens",
304+
zorder=-50,
305+
norm=colors.LogNorm(vmin=cutoff, vmax=df.value.max()),
306+
)
307+
return ax
274308

275309

276310
class TransformTools:

switch_model/transmission/transport/dispatch.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Defines model components to describe transmission dispatch for the
66
Switch model.
77
"""
8-
8+
import pandas as pd
99
from pyomo.environ import *
1010

1111
import os
@@ -136,7 +136,7 @@ def post_solve(instance, outdir):
136136
title="Transmission limit duals per period",
137137
note="Note: Outliers and zero-valued duals are ignored from box plot.",
138138
)
139-
def graph(tools):
139+
def transmission_limits(tools):
140140
dispatch = tools.get_dataframe("transmission_dispatch")
141141
dispatch = tools.transform.timestamp(dispatch)
142142
dispatch["transmission_limit_dual"] = tools.pd.to_numeric(
@@ -156,3 +156,43 @@ def graph(tools):
156156
ylabel="Transmission limit duals ($/MW)",
157157
showfliers=False,
158158
)
159+
160+
161+
@graph(
162+
"transmission_dispatch",
163+
title="Dispatched electricity over transmission lines during last period (in TWh)",
164+
note="Blue dots are net importers, red dots are net exports, greener lines indicate more use. Lines carrying <1TWh total not shown.",
165+
)
166+
def transmission_dispatch(tools):
167+
dispatch = tools.get_dataframe("transmission_dispatch.csv")
168+
dispatch = tools.transform.timestamp(dispatch).astype({"period": int})
169+
# Keep only the last period
170+
last_period = dispatch["period"].max()
171+
dispatch = dispatch[dispatch["period"] == last_period]
172+
dispatch = dispatch.rename(
173+
{
174+
"load_zone_from": "from",
175+
"load_zone_to": "to",
176+
"transmission_dispatch": "value",
177+
},
178+
axis=1,
179+
)
180+
dispatch["value"] *= (
181+
dispatch["tp_duration"] * 1e-6
182+
) # Change from power value to energy value
183+
dispatch = dispatch.groupby(["from", "to"], as_index=False)["value"].sum()
184+
ax = tools.maps.graph_transmission(dispatch, cutoff=1)
185+
exports = (
186+
dispatch[["from", "value"]].rename({"from": "gen_load_zone"}, axis=1).copy()
187+
)
188+
imports = dispatch[["to", "value"]].rename({"to": "gen_load_zone"}, axis=1).copy()
189+
imports["value"] *= -1
190+
exports = pd.concat([imports, exports])
191+
exports = exports.groupby("gen_load_zone", as_index=False).sum()
192+
tools.maps.graph_points(exports, ax)
193+
194+
195+
@graph("test")
196+
def test(tools):
197+
ax = tools.get_axes()
198+
tools.maps._plot_states(ax)

0 commit comments

Comments
 (0)