From a01b7b014b8f71abc186129a53a72f61800b9a10 Mon Sep 17 00:00:00 2001 From: fairliereese Date: Mon, 14 Mar 2022 11:38:26 -0700 Subject: [PATCH 1/3] added option to heatmap --- ternary/heatmapping.py | 45 ++++++++++++++++++++++++++++----- ternary/ternary_axes_subplot.py | 6 ++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/ternary/heatmapping.py b/ternary/heatmapping.py index 7487f69..73995bb 100644 --- a/ternary/heatmapping.py +++ b/ternary/heatmapping.py @@ -186,7 +186,7 @@ def polygon_generator(data, scale, style, permutation=None): yield map(project, vertices), value -def heatmap(data, scale, vmin=None, vmax=None, cmap=None, ax=None, +def heatmap(data, scale, vmin=None, vmax=None, adj_vlims=False, cmap=None, ax=None, scientific=False, style='triangular', colorbar=True, permutation=None, use_rgba=False, cbarlabel=None, cb_kwargs=None): """ @@ -203,6 +203,8 @@ def heatmap(data, scale, vmin=None, vmax=None, cmap=None, ax=None, The minimum color value, used to normalize colors. Computed if absent. vmax: float, None The maximum color value, used to normalize colors. Computed if absent. + adj_vlims: bool, False + Redefine min and max color values based on computed averages. cmap: String or matplotlib.colors.Colormap, None The name of the Matplotlib colormap to use. ax: Matplotlib AxesSubplot, None @@ -229,6 +231,12 @@ def heatmap(data, scale, vmin=None, vmax=None, cmap=None, ax=None, if not ax: fig, ax = plt.subplots() + + if vmax or vmin: + vlims_defined = True + else: + vlims_defined = False + # If use_rgba, make the RGBA values numpy arrays so that they can # be averaged. if use_rgba: @@ -244,6 +252,26 @@ def heatmap(data, scale, vmin=None, vmax=None, cmap=None, ax=None, if style not in ["t", "h", 'd']: raise ValueError("Heatmap style must be 'triangular', 'dual-triangular', or 'hexagonal'") + vertices_values = polygon_generator(data, scale, style, + permutation=permutation) + + # adjust limits of the colormapper if requested, + # but only if user also didn't request specific vlims + print('hewwo') + print(vmax) + print(vmin) + + if adj_vlims and not vlims_defined: + print('hewwo2') + vals = [] + for _, val in vertices_values: + vals.append(val) + vmin = min(vals) + vmax = max(vals) + + print(vmax) + print(vmin) + vertices_values = polygon_generator(data, scale, style, permutation=permutation) @@ -272,8 +300,8 @@ def heatmap(data, scale, vmin=None, vmax=None, cmap=None, ax=None, def heatmapf(func, scale=10, boundary=True, cmap=None, ax=None, scientific=False, style='triangular', colorbar=True, - permutation=None, vmin=None, vmax=None, cbarlabel=None, - cb_kwargs=None): + permutation=None, vmin=None, vmax=None, adj_vlims=False, + cbarlabel=None, cb_kwargs=None): """ Computes func on heatmap partition coordinates and plots heatmap. In other words, computes the function on lattice points of the simplex (normalized @@ -303,6 +331,8 @@ def heatmapf(func, scale=10, boundary=True, cmap=None, ax=None, The minimum color value, used to normalize colors. vmax: float The maximum color value, used to normalize colors. + adj_vlims: bool, False + Redefine min and max color values based on computed averages. cb_kwargs: dict dict of kwargs to pass to colorbar @@ -318,7 +348,8 @@ def heatmapf(func, scale=10, boundary=True, cmap=None, ax=None, # Pass everything to the heatmapper ax = heatmap(data, scale, cmap=cmap, ax=ax, style=style, scientific=scientific, colorbar=colorbar, - permutation=permutation, vmin=vmin, vmax=vmax, + permutation=permutation, vmin=vmin, vmax=vmax, + adj_vlims=adj_vlims, cbarlabel=cbarlabel, cb_kwargs=cb_kwargs) return ax @@ -347,8 +378,8 @@ def svg_polygon(coordinates, color): return polygon -def svg_heatmap(data, scale, filename, vmax=None, vmin=None, style='h', - permutation=None, cmap=None): +def svg_heatmap(data, scale, filename, vmax=None, vmin=None, adj_vlims=False, + style='h', permutation=None, cmap=None): """ Create a heatmap in SVG format. Intended for use with very large datasets, which would require large amounts of RAM using matplotlib. You can convert @@ -370,6 +401,8 @@ def svg_heatmap(data, scale, filename, vmax=None, vmin=None, style='h', The minimum color value, used to normalize colors. vmax: float The maximum color value, used to normalize colors. + adj_vlims: bool, False + Redefine min and max color values based on computed averages. cmap: String or matplotlib.colors.Colormap, None The name of the Matplotlib colormap to use. style: String, "h" diff --git a/ternary/ternary_axes_subplot.py b/ternary/ternary_axes_subplot.py index dd56f9b..f28a814 100644 --- a/ternary/ternary_axes_subplot.py +++ b/ternary/ternary_axes_subplot.py @@ -436,7 +436,7 @@ def plot_colored_trajectory(self, points, cmap=None, **kwargs): def heatmap(self, data, scale=None, cmap=None, scientific=False, style='triangular', colorbar=True, use_rgba=False, - vmin=None, vmax=None, cbarlabel=None, cb_kwargs=None): + vmin=None, vmax=None, adj_vlims=False, cbarlabel=None, cb_kwargs=None): permutation = self._permutation if not scale: scale = self.get_scale() @@ -446,12 +446,12 @@ def heatmap(self, data, scale=None, cmap=None, scientific=False, heatmapping.heatmap(data, scale, cmap=cmap, style=style, ax=ax, scientific=scientific, colorbar=colorbar, permutation=permutation, use_rgba=use_rgba, - vmin=vmin, vmax=vmax, cbarlabel=cbarlabel, + vmin=vmin, vmax=vmax, adj_vlims=adj_vlims, cbarlabel=cbarlabel, cb_kwargs=cb_kwargs) def heatmapf(self, func, scale=None, cmap=None, boundary=True, style='triangular', colorbar=True, scientific=False, - vmin=None, vmax=None, cbarlabel=None, cb_kwargs=None): + vmin=None, vmax=None, adj_vlims=False, cbarlabel=None, cb_kwargs=None): if not scale: scale = self.get_scale() if style.lower()[0] == 'd': From 7c71f575abf21b798f65345fc1dfbe0080a79977 Mon Sep 17 00:00:00 2001 From: fairliereese Date: Mon, 14 Mar 2022 11:41:23 -0700 Subject: [PATCH 2/3] removed prints --- ternary/heatmapping.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ternary/heatmapping.py b/ternary/heatmapping.py index 73995bb..f65c034 100644 --- a/ternary/heatmapping.py +++ b/ternary/heatmapping.py @@ -257,21 +257,13 @@ def heatmap(data, scale, vmin=None, vmax=None, adj_vlims=False, cmap=None, ax=No # adjust limits of the colormapper if requested, # but only if user also didn't request specific vlims - print('hewwo') - print(vmax) - print(vmin) - if adj_vlims and not vlims_defined: - print('hewwo2') vals = [] for _, val in vertices_values: vals.append(val) vmin = min(vals) vmax = max(vals) - - print(vmax) - print(vmin) - + vertices_values = polygon_generator(data, scale, style, permutation=permutation) From f4e4285898f4c1de89ee518251ab6246c30d3a7a Mon Sep 17 00:00:00 2001 From: fairliereese Date: Wed, 24 Aug 2022 12:20:02 -0700 Subject: [PATCH 3/3] fixed non-overlapping triangles issue --- ternary/heatmapping.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ternary/heatmapping.py b/ternary/heatmapping.py index f65c034..68146a4 100644 --- a/ternary/heatmapping.py +++ b/ternary/heatmapping.py @@ -263,7 +263,7 @@ def heatmap(data, scale, vmin=None, vmax=None, adj_vlims=False, cmap=None, ax=No vals.append(val) vmin = min(vals) vmax = max(vals) - + vertices_values = polygon_generator(data, scale, style, permutation=permutation) @@ -277,7 +277,7 @@ def heatmap(data, scale, vmin=None, vmax=None, adj_vlims=False, cmap=None, ax=No color = value # rgba tuple (r,g,b,a) all in [0,1] # Matplotlib wants a list of xs and a list of ys xs, ys = unzip(vertices) - ax.fill(xs, ys, facecolor=color, edgecolor=color) + ax.fill(xs, ys, facecolor=color, edgecolor=None) if not cb_kwargs: cb_kwargs = dict()