-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
442 lines (369 loc) · 16.4 KB
/
plot.py
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
## Copyright 2015-2017 Tom Brown (FIAS), Jonas Hoersch (FIAS)
## Copyright 2017 João Gorenstein Dedecca
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 3 of the
## License, or (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Functions for plotting networks.
"""
# make the code as Python 3 compatible as possible
from __future__ import division
from __future__ import absolute_import
import six
from six import iteritems
import pandas as pd
import numpy as np
__author__ = """
Tom Brown (FIAS), Jonas Hoersch (FIAS)
Joao Gorenstein Dedecca
"""
__copyright__ = """
Copyright 2015-2017 Tom Brown (FIAS), Jonas Hoersch (FIAS), GNU GPL 3
Copyright 2017 João Gorenstein Dedecca, GNU GPL 3
"""
plt_present = True
try:
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
from matplotlib.collections import LineCollection, PatchCollection
except:
plt_present = False
basemap_present = True
try:
from mpl_toolkits.basemap import Basemap
except:
basemap_present = False
def plot(network, margin=0.05, ax=None, basemap=True, bus_colors='b',
line_colors='g', bus_sizes=10, line_widths=2, title="",
line_cmap=None, bus_cmap=None, boundaries=None,
geometry=False, branch_components=['Line', 'Link']):
"""
Plot the network buses and lines using matplotlib and Basemap.
Parameters
----------
margin : float
Margin at the sides as proportion of distance between max/min x,y
ax : matplotlib ax, defaults to plt.gca()
Axis to which to plot the network
basemap : bool, default True
Switch to use Basemap
bus_colors : dict/pandas.Series
Colors for the buses, defaults to "b"
bus_sizes : dict/pandas.Series
Sizes of bus points, defaults to 10
line_colors : dict/pandas.Series
Colors for the lines, defaults to "g" for Lines and "cyan" for
Links. Colors for branches other than Lines can be
specified using a pandas Series with a MultiIndex.
line_widths : dict/pandas.Series
Widths of lines, defaults to 2. Widths for branches other
than Lines can be specified using a pandas Series with a
MultiIndex.
title : string
Graph title
line_cmap : plt.cm.ColorMap/str|dict
If line_colors are floats, this color map will assign the colors.
Use a dict to specify colormaps for more than one branch type.
bus_cmap : plt.cm.ColorMap/str
If bus_colors are floats, this color map will assign the colors
boundaries : list of four floats
Boundaries of the plot in format [x1,x2,y1,y2]
branch_components : list of str
Branch components to be plotted, defaults to Line and Link.
Returns
-------
bus_collection, branch_collection1, ... : tuple of Collections
Collections for buses and branches.
"""
defaults_for_branches = {
'Link': dict(color="cyan", width=2),
'Line': dict(color="b", width=2),
'Transformer': dict(color='green', width=2)
}
if not plt_present:
logger.error("Matplotlib is not present, so plotting won't work.")
return
if ax is None:
ax = plt.gca()
def compute_bbox_with_margins(margin, x, y):
#set margins
pos = np.asarray((x, y))
minxy, maxxy = pos.min(axis=1), pos.max(axis=1)
xy1 = minxy - margin*(maxxy - minxy)
xy2 = maxxy + margin*(maxxy - minxy)
return tuple(xy1), tuple(xy2)
x = network.buses["x"]
y = network.buses["y"]
if basemap and basemap_present:
if boundaries is None:
(x1, y1), (x2, y2) = compute_bbox_with_margins(margin, x, y)
else:
x1, x2, y1, y2 = boundaries
bmap = Basemap(resolution='l', epsg=network.srid,
llcrnrlat=y1, urcrnrlat=y2, llcrnrlon=x1,
urcrnrlon=x2, ax=ax)
bmap.drawcountries()
bmap.drawcoastlines()
x, y = bmap(x.values, y.values)
x = pd.Series(x, network.buses.index)
y = pd.Series(y, network.buses.index)
if isinstance(bus_sizes, pd.Series) and isinstance(bus_sizes.index, pd.MultiIndex):
# We are drawing pies to show all the different shares
assert len(network.buses.index.difference(bus_sizes.index.levels[0])) == 0, \
"The first MultiIndex level of bus_sizes must contain buses"
assert isinstance(bus_colors, dict) and set(bus_colors).issuperset(bus_sizes.index.levels[1]), \
"bus_colors must be a dictionary defining a color for each element " \
"in the second MultiIndex level of bus_sizes"
bus_sizes = bus_sizes.sort_index(level=0, sort_remaining=False)
patches = []
for b_i in bus_sizes.index.levels[0]:
s = bus_sizes.loc[b_i]
radius = s.sum()**0.5
ratios = s/s.sum()
start = 0.25
for i, ratio in ratios.iteritems():
patches.append(Wedge((x.at[b_i], y.at[b_i]), radius,
360*start, 360*(start+ratio),
facecolor=bus_colors[i]))
start += ratio
bus_collection = PatchCollection(patches, match_original=True)
ax.add_collection(bus_collection)
else:
c = pd.Series(bus_colors, index=network.buses.index)
if c.dtype == np.dtype('O'):
c.fillna("b", inplace=True)
c = list(c.values)
s = pd.Series(bus_sizes, index=network.buses.index, dtype="float").fillna(10)
bus_collection = ax.scatter(x, y, c=c, s=s, cmap=bus_cmap)
def as_branch_series(ser):
if isinstance(ser, dict) and set(ser).issubset(branch_components):
return pd.Series(ser)
elif isinstance(ser, pd.Series):
if isinstance(ser.index, pd.MultiIndex):
return ser
index = ser.index
ser = ser.values
else:
index = network.lines.index
return pd.Series(ser,
index=pd.MultiIndex(levels=(["Line"], index),
labels=(np.zeros(len(index)),
np.arange(len(index)))))
line_colors = as_branch_series(line_colors)
line_widths = as_branch_series(line_widths)
if not isinstance(line_cmap, dict):
line_cmap = {'Line': line_cmap}
branch_collections = []
for c in network.iterate_components(branch_components):
l_defaults = defaults_for_branches[c.name]
l_widths = line_widths.get(c.name, l_defaults['width'])
l_nums = None
l_colors = line_colors.get(c.name, l_defaults['color'])
if isinstance(l_colors, pd.Series):
if issubclass(l_colors.dtype.type, np.number):
l_nums = l_colors
l_colors = None
else:
l_colors.fillna(l_defaults['color'], inplace=True)
if not geometry:
segments = (np.asarray(((c.df.bus0.map(x),
c.df.bus0.map(y)),
(c.df.bus1.map(x),
c.df.bus1.map(y))))
.transpose(2, 0, 1))
else:
from shapely.wkt import loads
from shapely.geometry import LineString
linestrings = c.df.geometry.map(loads)
assert all(isinstance(ls, LineString) for ls in linestrings), \
"The WKT-encoded geometry in the 'geometry' column must be composed of LineStrings"
segments = np.asarray(list(linestrings.map(np.asarray)))
if basemap and basemap_present:
segments = np.transpose(bmap(*np.transpose(segments, (2, 0, 1))), (1, 2, 0))
l_collection = LineCollection(segments,
linewidths=l_widths,
antialiaseds=(1,),
colors=l_colors,
transOffset=ax.transData)
if l_nums is not None:
l_collection.set_array(np.asarray(l_nums))
l_collection.set_cmap(line_cmap.get(c.name, None))
l_collection.autoscale()
ax.add_collection(l_collection)
l_collection.set_zorder(1)
branch_collections.append(l_collection)
bus_collection.set_zorder(2)
ax.update_datalim(compute_bbox_with_margins(margin, x, y))
ax.autoscale_view()
ax.set_title(title)
return (bus_collection,) + tuple(branch_collections)
def plot_uni(network, margin=0.05, ax=None, basemap=True, bus_colors='b',
line_colors='r', line_styles = 'solid',bus_sizes=35, line_widths=2, title="",
line_cmap=None, bus_cmap=None, boundaries=None,
geometry=False, branch_components=['Line', 'Link'],bc_min = None, bc_max = None, bus_line_widths = 1, bus_markers = 'o',edgecolors='k'):
"""
Plot the network buses and lines using matplotlib and Basemap.
Parameters
----------
margin : float
Margin at the sides as proportion of distance between max/min x,y
ax : matplotlib ax, defaults to plt.gca()
Axis to which to plot the network
basemap : bool, default True
Switch to use Basemap
bus_colors : dict/pandas.Series
Colors for the buses, defaults to "b"
bus_sizes : dict/pandas.Series
Sizes of bus points, defaults to 10
line_colors : dict/pandas.Series
Colors for the lines, defaults to "g" for Lines and "cyan" for
Links. Colors for branches other than Lines can be
specified using a pandas Series with a MultiIndex.
line_widths : dict/pandas.Series
Widths of lines, defaults to 2. Widths for branches other
than Lines can be specified using a pandas Series with a
MultiIndex.
title : string
Graph title
line_cmap : plt.cm.ColorMap/str|dict
If line_colors are floats, this color map will assign the colors.
Use a dict to specify colormaps for more than one branch type.
bus_cmap : plt.cm.ColorMap/str
If bus_colors are floats, this color map will assign the colors
boundaries : list of four floats
Boundaries of the plot in format [x1,x2,y1,y2]
branch_types : list of str or pypsa.component
Branch types to be plotted, defaults to Line and Link.
Returns
-------
bus_collection, branch_collection1, ... : tuple of Collections
Collections for buses and branches.
"""
defaults_for_branches = {
'Link': dict(color="cyan", width=2,style='solid'),
'Line': dict(color="r", width=2,style='solid')
}
if not plt_present:
logger.error("Matplotlib is not present, so plotting won't work.")
return
if ax is None:
ax = plt.gca()
def compute_bbox_with_margins(margin, x, y):
#set margins
pos = np.asarray((x, y))
minxy, maxxy = pos.min(axis=1), pos.max(axis=1)
xy1 = minxy - margin*(maxxy - minxy)
xy2 = maxxy + margin*(maxxy - minxy)
return tuple(xy1), tuple(xy2)
x = network.buses["x"]
y = network.buses["y"]
if basemap and basemap_present:
if boundaries is None:
(x1, y1), (x2, y2) = compute_bbox_with_margins(margin, x, y)
else:
x1, x2, y1, y2 = boundaries
bmap = Basemap(resolution='i', epsg=network.srid,
llcrnrlat=y1, urcrnrlat=y2, llcrnrlon=x1,
urcrnrlon=x2, ax=ax)
bmap.drawcountries(zorder=1)
bmap.drawcoastlines(zorder=1,linewidth=0.5)
bmap.fillcontinents(color='0.9',zorder=0)
x, y = bmap(x.values, y.values)
x = pd.Series(x, network.buses.index)
y = pd.Series(y, network.buses.index)
c = pd.Series(bus_colors, index=network.buses.index)
bus_mask = network.buses[network.buses.carrier != 'DC'].index
if c.dtype == np.dtype('O'):
c.fillna("b", inplace=True)
#c = list(c.values)
s = pd.Series(bus_sizes, index=network.buses.index, dtype="float").fillna(10)
m = pd.Series(bus_markers, index=network.buses.index).fillna('o')
for marker in m.unique():
buses = m[m == marker].index.intersection(bus_mask)
bus_collection = ax.scatter(x[buses], y[buses], c=c[buses], s=s[buses], marker = marker,linewidths=bus_line_widths,edgecolors=edgecolors, cmap=bus_cmap,vmin = bc_min,vmax = bc_max,zorder = 3)
def as_branch_series(ser):
if isinstance(ser, pd.Series):
if isinstance(ser.index, pd.MultiIndex):
return ser
index = ser.index
ser = ser.values
else:
index = network.lines.index
return pd.Series(ser,
index=pd.MultiIndex(levels=(["Line"], index),
labels=(np.zeros(len(index)),
np.arange(len(index)))))
line_colors = as_branch_series(line_colors)
line_widths = as_branch_series(line_widths)
line_styles = as_branch_series(line_styles)
if not isinstance(line_cmap, dict):
line_cmap = {'Line': line_cmap, 'Link': line_cmap}
branch_collections = []
for c in network.iterate_components(branch_components):
l_defaults = defaults_for_branches[c.name]
l_widths = line_widths.get(c.name, l_defaults['width'])
# try:
# l_widths
# except NameError:
# l_widths = line_widths.get(t.name, l_defaults['width'])
# else:
# l_widths = l_widths.append(line_widths.get(t.name, l_defaults['width']))
l_nums = None
if c.name in line_colors:
l_colors = line_colors[c.name]
if issubclass(l_colors.dtype.type, np.number):
l_nums = l_colors
l_colors = None
else:
l_colors.fillna(l_defaults['color'], inplace=True)
else:
l_colors = l_defaults['color']
if c.name in line_styles:
l_styles= line_styles[c.name]
if issubclass(l_styles.dtype.type, np.number):
l_nums = l_styles
l_styles = None
else:
l_styles.fillna(l_defaults['style'], inplace=True)
else:
l_styles = l_defaults['style']
if not geometry:
segments = (np.asarray(((c.df.bus0.map(x),
c.df.bus0.map(y)),
(c.df.bus1.map(x),
c.df.bus1.map(y))))
.transpose(2, 0, 1))
else:
from shapely.wkt import loads
from shapely.geometry import LineString
linestrings = c.df.geometry.map(loads)
assert all(isinstance(ls, LineString) for ls in linestrings), \
"The WKT-encoded geometry in the 'geometry' column must be composed of LineStrings"
segments = np.asarray(list(linestrings.map(np.asarray)))
if basemap and basemap_present:
segments = np.transpose(bmap(*np.transpose(segments, (2, 0, 1))), (1, 2, 0))
l_collection = LineCollection(segments,
linewidths=l_widths,
antialiaseds=(1,),
linestyles=l_styles,
colors=l_colors,
alpha = .8,
transOffset=ax.transData)
if l_nums is not None:
l_collection.set_array(np.asarray(l_nums))
l_collection.set_cmap(line_cmap.get(c.name, None))
l_collection.autoscale()
ax.add_collection(l_collection)
l_collection.set_zorder(2)
branch_collections.append(l_collection)
bus_collection.set_zorder(3)
ax.update_datalim(compute_bbox_with_margins(margin, x, y))
ax.autoscale_view()
ax.set_title(title)
return (bus_collection,) + tuple(branch_collections)