-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_helper.py
More file actions
67 lines (58 loc) · 2.23 KB
/
plot_helper.py
File metadata and controls
67 lines (58 loc) · 2.23 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
import shapely.geometry as sg # geometric objects
import plotly.express as px # plotting
import plotly.graph_objects as po # plotting
import numpy as np # array computations
def plot_geom(geom, fig=None, name=None) -> po.Figure:
"""Plot a single shapely object."""
if fig is None:
fig = po.Figure()
match geom: # decide how to plot depending on type of geometry
case sg.LineString():
xy = np.asarray(geom.xy)
fig.add_scatter(x=xy[0], y=xy[1], name=name, mode="lines")
case sg.Polygon():
xy = np.concatenate(
[
np.asarray(geom.exterior.xy),
[[np.nan], [np.nan]],
*[np.asarray(i.xy) for i in geom.interiors],
],
axis=1,
)
fig.add_scatter(x=xy[0], y=xy[1], fill="toself", name=name, mode="lines")
# for Multi* objects combine all parts into a single one devided by NaN values to create a gap
case sg.MultiLineString():
all_xy = [
np.concatenate([np.asarray(g.xy), [[np.nan], [np.nan]]], axis=1)
for g in geom.geoms
]
xy = np.concatenate(all_xy, axis=1)
fig.add_scatter(x=xy[0], y=xy[1], name=name, mode="lines")
case sg.MultiPolygon():
all_xy = [
np.concatenate(
[
np.asarray(g.exterior.xy),
[[np.nan], [np.nan]],
*[np.asarray(i.xy) for i in g.interiors],
[[np.nan], [np.nan]],
],
axis=1,
)
for g in geom.geoms
]
xy = np.concatenate(all_xy, axis=1)
fig.add_scatter(x=xy[0], y=xy[1], fill="toself", name=name, mode="lines")
fig.update_yaxes( # set equally scaled axes
scaleanchor="x",
scaleratio=1,
)
return fig
def plot_geoms(geoms, names=None) -> po.Figure:
"""Plot multiple shapely objects in a single figure."""
fig = po.Figure()
if not names:
names = [None for g in geoms]
for g, n in zip(geoms, names):
plot_geom(g, fig, name=n)
return fig