Repository navigation
Expand file tree
/
Copy pathspatial_heatmap.py
More file actions
195 lines (174 loc) · 7.38 KB
/
Copy pathspatial_heatmap.py
File metadata and controls
195 lines (174 loc) · 7.38 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
#
# This file is part of the Buildings and Habitats object Model (BHoM)
# Copyright (c) 2015 - 2026, the respective contributors. All rights reserved.
#
# Each contributor holds copyright over their respective contributions.
# The project versioning (Git) records all such contribution source information.
#
#
# The BHoM is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3.0 of the License, or
# (at your option) any later version.
#
# The BHoM 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
#
"""Methods for plotting spatial heatmaps."""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import BoundaryNorm, Colormap
from matplotlib.figure import Figure
from matplotlib.tri import Triangulation
from mpl_toolkits.axes_grid1 import make_axes_locatable
from ..bhom.analytics import bhom_analytics
@bhom_analytics()
def spatial_heatmap(
triangulations: list[Triangulation],
values: list[list[float]],
levels: list[float] | int = None,
contours: list[float] = None,
contour_colors: list[str] = None,
contour_widths: list[float] = None,
cmap: Colormap = "viridis",
extend: str = "neither",
norm: BoundaryNorm = None,
xlims: list[float] = None,
ylims: list[float] = None,
colorbar_label: str = "",
title: str = "",
highlight_pts: dict[str, tuple[int]] = None,
show_legend_title: bool = True,
clabels: bool = False,
style_context:str = "python_toolkit.bhom",
) -> Figure:
"""Plot a spatial map of a variable using a triangulation and associated values.
Args:
triangulations (list[Triangulation]):
A list of triangulations to plot.
values (list[list[float]]):
A list of values, corresponding with the triangulations and their respective indices.
levels (list[float] | int, optional):
The number of levels to include in the colorbar. Defaults to None which will use
10-steps between the min/max for all given values.
contours (list[float], optional):
Add contours at the given values to the spatial plot. Defaults to None.
contour_colors (list[str], optional):
Color each of the listed contours. Defaults to None.
contour_widths (list[float], optional):
The width each of the listed contours. Defaults to None.
cmap (Colormap, optional):
The colormap to use for this plot. Defaults to "viridis".
extend (str, optional):
Define how to handle the end-points of the colorbar. Defaults to "neither".
norm (BoundaryNorm, optional):
A matplotlib BoundaryNorm object containing colormap boundary mapping information.
Defaults to None.
xlims (list[float], optional):
The x-limit for the plot. Defaults to None.
ylims (list[float], optional):
The y-limit for the plot. Defaults to None.
colorbar_label (str, optional):
A label to be placed next to the colorbar. Defaults to "".
title (str, optional):
The title to be placed on the plot. Defaults to "".
highlight_pts (dict[str, int], optional):
A set of points (and their names) to indicate on the spatial plot. Value is the int
index of the highlighted point.
show_legend_title (bool, optional):
A convenient flag to hide the legend and title.
clabels (bool, optional):
A flag to show contour labels. Defaults to False.
style_context (string, optional):
The matplotlib style to use. Defaults to python_toolkit.bhom.
Returns:
Figure: A matplotlib Figure object.
"""
for tri, zs in list(zip(*[triangulations, values])):
if len(tri.x) != len(zs):
raise ValueError(
"The shape of the triangulations and values given do not match."
)
if levels is None:
levels = np.linspace(
min(np.amin(i) for i in values), max(np.amax(i) for i in values), 10
)
if xlims is None:
xlims = [
min(i.x.min() for i in triangulations),
max(i.x.max() for i in triangulations),
]
if ylims is None:
ylims = [
min(i.y.min() for i in triangulations),
max(i.y.max() for i in triangulations),
]
with plt.style.context(style_context):
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax.set_aspect("equal")
ax.axis("off")
ax.set_xlim(xlims)
ax.set_ylim(ylims)
tcls = []
for tri, zs in list(zip(*[triangulations, values])):
tcf = ax.tricontourf(
tri, zs, extend=extend, cmap=cmap, levels=levels, norm=norm
)
# add contour lines
if contours is not None:
if not (
all(i < np.amin(zs) for i in contours)
or all(i > np.amax(zs) for i in contours)
):
if contour_widths is None:
contour_widths = [1.5] * len(contours)
if contour_colors is None:
contour_colors = ["k"] * len(contours)
if len(contour_colors) != len(contours) != len(contour_widths):
raise ValueError("contour vars must be same length")
tcl = ax.tricontour(
tri,
zs,
levels=contours,
colors=contour_colors,
linewidths=contour_widths,
)
if clabels:
ax.clabel(tcl, inline=1, fontsize="small", colors=contour_colors)
tcls.append(tcl)
if highlight_pts is not None:
if len(triangulations) > 1:
raise ValueError(
"Point highlighting is only possible for 1-length triangulations."
)
pt_size = (xlims[1] - xlims[0]) / 5
for k, v in highlight_pts.items():
ax.scatter(
triangulations[0].x[v], triangulations[0].y[v], s=pt_size, c="red"
)
ax.text(
triangulations[0].x[v] + (pt_size / 10),
triangulations[0].y[v],
k,
ha="left",
va="center",
)
if show_legend_title:
# Plot colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.1, aspect=20)
cbar = plt.colorbar(
tcf, cax=cax # , format=mticker.StrMethodFormatter("{x:04.1f}")
)
cbar.outline.set_visible(False)
cbar.set_label(colorbar_label)
for tcl in tcls:
cbar.add_lines(tcl)
ax.set_title(title, ha="left", va="bottom", x=0)
plt.tight_layout()
return fig