-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplot.py
115 lines (88 loc) · 4.07 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
import numpy as np
from matplotlib.cm import get_cmap
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
import networkx as nx
from constants import ZONING_NAMES, CMAP_ZONING, CMAP_ROOMTYPE
FS = 10
AWESOME_COLORS = ["#1932E1"] # dark blue, dark green, light green, dark yellow
COLORSET = 'tab20'
def set_figure(nc, nr,
fs=10,
fs_title=7.5,
fs_legend=10,
fs_xtick=3,
fs_ytick=3,
fs_axes=4,
ratio=1):
"""
Custom figure setup function that generates a nicely looking figure outline.
It includes "making-sense"-fontsizes across all text locations (e.g. title, axes).
You can always change things later yourself through the outputs or plt.rc(...).
"""
fig, axs = plt.subplots(ncols=nc, nrows=nr, figsize=(fs*nc*ratio, fs*nr))
try:
axs = axs.flatten()
except:
pass
plt.rc("figure", titlesize=fs*fs_title)
plt.rc("legend", fontsize=fs*fs_legend)
plt.rc("xtick", labelsize=fs*fs_xtick)
plt.rc("ytick", labelsize=fs*fs_ytick)
plt.rc("axes", labelsize=fs*fs_axes, titlesize=fs*fs_title)
return fig, axs
def plot_polygon(ax, poly, label=None, **kwargs):
x, y = poly.exterior.xy
ax.fill(x, y, label=label, **kwargs)
return
def plot_shapes(ax, polygons, colors, **kwargs):
for poly, color in zip(polygons, colors):
plot_polygon(ax, Polygon(poly), color=color, **kwargs)
def plot_graph(G, ax, c_node='black', c_edge=['white']*4, dw_edge=False, pos=None, node_size=10,
edge_size=10):
"""
Plots the adjacency or access graph of a floor plan's corresponding graph structure.
"""
# position
if pos is None:
pos = nx.spring_layout(G, seed=7) # positions for all nodes - seed for reproducibility
# nodes
nx.draw_networkx_nodes(G, pos, node_size=node_size, node_color=c_node, ax=ax)
# edges
if dw_edge:
epass = [(u, v) for (u, v, d) in G.edges(data=True) if d["connectivity"] == 'passage']
edoor = [(u, v) for (u, v, d) in G.edges(data=True) if d["connectivity"] == 'door']
efront = [(u, v) for (u, v, d) in G.edges(data=True) if d["connectivity"] == 'entrance']
# red full for passage, red dashed for door, yellow dashed for front
nx.draw_networkx_edges(G, pos, edgelist=epass, edge_color=c_edge[1],
width=edge_size, ax=ax)
nx.draw_networkx_edges(G, pos, edgelist=edoor, edge_color=c_edge[2],
width=edge_size, style="dashed", ax=ax)
nx.draw_networkx_edges(G, pos, edgelist=efront, edge_color=c_edge[3],
width=edge_size, style="-.", ax=ax)
else:
nx.draw_networkx_edges(G, pos, edge_color=c_edge[0],
width=edge_size, ax=ax)
ax.axis('off')
def plot_floor(G, ax, node_size=50, edge_size=3):
"""Plots a floor plan's corresponding access graph. Including room shapes."""
# Sets cmap
attribute_names = list(G.nodes[1].keys())
column = 'room_type' if 'room_type' in attribute_names else 'zoning_type'
cmap = CMAP_ROOMTYPE if column == 'room_type' else CMAP_ZONING
# Extracts node shape, color and position
shapes = [Polygon(n) for _, n in G.nodes('geometry')]
colors = [np.array(cmap(n)).reshape(1,4) for _, n in G.nodes(column)]
pos = {n: np.array(G.nodes[n]['centroid']) for n in G.nodes}
# Draw shapes
plot_shapes(ax, shapes, colors, ec="black", lw=0, alpha=1)
# Draw nodes
nx.draw_networkx_nodes(G, pos, node_size=node_size, node_color='black', ax=ax)
# Draw edges (door and passage)
edges = [(u, v) for (u, v, d) in G.edges(data="connectivity") if d in ["door", "passage"]]
nx.draw_networkx_edges(G, pos, edgelist=edges, edge_color='black',
width=edge_size, ax=ax)
# Draw edges (entrance)
edges = [(u, v) for (u, v, d) in G.edges(data="connectivity") if d == "entrance"]
nx.draw_networkx_edges(G, pos, edgelist=edges, edge_color='red',
width=edge_size*2, ax=ax)