Skip to content

Commit

Permalink
[plots.histogram_w_smoothing_auto] new optargs alpha_below, maxcount,…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
gph82 committed Nov 6, 2023
1 parent 2f0d6b9 commit 8cb619c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
34 changes: 26 additions & 8 deletions mdciao/plots/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def plot_w_smoothing_auto(y, ax=None, label=None, color=None, x=None, background

def histogram_w_smoothing_auto(data, bins=10, ax=None,
smooth_bw=True, background=True, fill_below=True,
color=None, label=None) -> _plt.Axes:
color=None, label=None,
alpha_below=.25, maxcount=False) -> _plt.Axes:
r"""
Plot a histogram of `data` with possibilities of smoothing and filling the area below
Expand All @@ -163,7 +164,7 @@ def histogram_w_smoothing_auto(data, bins=10, ax=None,
the current axis will be used invoking
:obj:`~matplotlib.pyplot.gca`. If there's no
current axis, one will be created.
smooth_bw : bool or float, default is False
smooth_bw : bool or float, default is True
If True, smooth the histogram using a
Gaussian-kernel-density estimation with
an estimator bandwidth of .5 (Angstrom).
Expand All @@ -188,8 +189,21 @@ def histogram_w_smoothing_auto(data, bins=10, ax=None,
label : str or None, default is None
The label for the data, which will
be shown in the legend
alpha_below : float, default is .25
The are below the curve will
be filled with this alpha (transparency)
value. Only has an effect if `fill_below`
is True
maxcount : bool or positive float, default is False
Normalize when plotting the histogram,
s.t. different datasets can be plotted
together at the same height even with
very different number of absolute
counts. If True, counts will be normalized
to the maximum number of counts, s.t.
histograms will peak at 1. If any other
positive value, that's where the peak
will be.
Returns
-------
Expand All @@ -200,15 +214,19 @@ def histogram_w_smoothing_auto(data, bins=10, ax=None,
ax = _plt.gca()

h, bin_edges = _np.histogram(data, bins=bins)
if maxcount:
h = h/h.max() * maxcount # multiply by boolean if True means multiply by one, if scalar by the value
x = (bin_edges[:-1] + bin_edges[1:]) / 2
if smooth_bw:
if isinstance(smooth_bw, bool):
smooth_bw = .5
model = _GKDE(data, bw_method=smooth_bw)
xs = _np.linspace(data.min(), data.max(), num=500)
xs = _np.linspace(_np.min(data), _np.max(data), num=500)
ys = model.evaluate(xs)
ys /= ys.max()
ys *= h.max()
if maxcount:
y = ys/ys.max() * maxcount
line = ax.plot(xs, ys, label=label, color=color)[0]
if fill_below:
ax.fill_between(xs, ys, alpha=.1, color=line.get_color())
Expand All @@ -220,9 +238,9 @@ def histogram_w_smoothing_auto(data, bins=10, ax=None,
else:
line = ax.plot(x, h, label=label, color=color)[0]
if fill_below:
ax.fill_between(x, h, alpha=.1, color=line.get_color())

ax.legend()
ax.fill_between(x, h, alpha=alpha_below, color=line.get_color())
if label is not None:
ax.legend()

return ax

Expand Down
8 changes: 8 additions & 0 deletions tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ def test_interface(self):
ifig, __, __ = plots.compare_groups_of_contacts({"1": intf1, "2":intf2}, ctc_cutoff_Ang=30,interface=True)
#ifig.savefig("test.png")

class test_histogram_w_smoothing_auto(unittest.TestCase):

def test_just_runs_all(self):
ax = plots.histogram_w_smoothing_auto(_np.random.randn(200), bins=20, maxcount=3.14)
for line in ax.lines:
y = line.get_xydata()[:,1]
assert y.max()==3.14


class Test_plot_w_smoothing_auto(unittest.TestCase):

Expand Down

0 comments on commit 8cb619c

Please sign in to comment.