a b/ants/plotting/plot_hist.py
1
"""
2
Functions for plotting ants images
3
"""
4
5
6
__all__ = [
7
    "plot_hist"
8
]
9
10
import fnmatch
11
import math
12
import os
13
import warnings
14
15
from matplotlib import gridspec
16
import matplotlib.pyplot as plt
17
import matplotlib.patheffects as path_effects
18
import matplotlib.lines as mlines
19
import matplotlib.patches as patches
20
import matplotlib.mlab as mlab
21
import matplotlib.animation as animation
22
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
23
24
25
import numpy as np
26
from ants.decorators import image_method
27
28
@image_method
29
def plot_hist(
30
    image,
31
    threshold=0.0,
32
    fit_line=False,
33
    normfreq=True,
34
    ## plot label arguments
35
    title=None,
36
    grid=True,
37
    xlabel=None,
38
    ylabel=None,
39
    ## other plot arguments
40
    facecolor="green",
41
    alpha=0.75,
42
):
43
    """
44
    Plot a histogram from an ANTsImage
45
46
    Arguments
47
    ---------
48
    image : ANTsImage
49
        image from which histogram will be created
50
    """
51
    img_arr = image.numpy().flatten()
52
    img_arr = img_arr[np.abs(img_arr) > threshold]
53
54
    if normfreq != False:
55
        normfreq = 1.0 if normfreq == True else normfreq
56
    n, bins, patches = plt.hist(
57
        img_arr, 50, facecolor=facecolor, alpha=alpha
58
    )
59
60
    if fit_line:
61
        # add a 'best fit' line
62
        y = mlab.normpdf(bins, img_arr.mean(), img_arr.std())
63
        l = plt.plot(bins, y, "r--", linewidth=1)
64
65
    if xlabel is not None:
66
        plt.xlabel(xlabel)
67
    if ylabel is not None:
68
        plt.ylabel(ylabel)
69
    if title is not None:
70
        plt.title(title)
71
72
    plt.grid(grid)
73
    plt.show()