Skip to content

Commit

Permalink
added func for overlaying numerical values on a 2D heatmap image
Browse files Browse the repository at this point in the history
  • Loading branch information
jgostick committed Sep 5, 2023
1 parent 401c892 commit bfb5c1a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions porespy/visualization/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,32 @@
'satn_to_movie',
'satn_to_panels',
'prep_for_imshow',
'annotate_heatmap',
]


def annotate_heatmap(heatmap, ax, mask=None, fontdict={}):
im = heatmap
font_kws = {
'size': 10,
}
font_kws.update(fontdict)
if mask is None:
mask = np.ones_like(im, dtype=bool)
if im.dtype == bool:
im = np.array(im, dtype=int)
c_lo, c_hi = np.amin(im[mask]), np.amax(im[mask])
c_norm = (im - c_lo)/(c_hi - c_lo)
for i in range(im.shape[0]):
for j in range(im.shape[1]):
if mask[i, j]:
ax.text(j+0.5, i+0.5, im[i, j],
ha='center', va='center',
color='k' if (0.1 <= c_norm[i, j] <= 0.9) else 'w',
**font_kws)
return ax


def set_mpl_style(): # pragma: no cover
r"""
Prettifies matplotlib's output by adjusting fonts, markersize etc.
Expand Down

0 comments on commit bfb5c1a

Please sign in to comment.