Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions histograms/histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import random
import sys
import warnings
from collections import Counter

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -86,14 +85,20 @@ def ascii_histogram(seq) -> None:
print("bin_edges:", bin_edges)

bcounts = np.bincount(a)
hist, _ = np.histogram(a, range=(0, max(a)), bins=max(a) + 1)
hist, _ = np.histogram(a, range=(0, np.max(a)), bins=np.max(a) + 1)
print(bcounts)
assert np.array_equal(hist, bcounts), "Bincounts unequal."

# Reproducing `collections.Counter`
print(
"Reproducing `collections.Counter`:",
dict(zip(np.unique(a), bcounts[bcounts.nonzero()], strict=False)),
dict(
zip(
np.unique(a).tolist(),
bcounts[bcounts.nonzero()].tolist(),
strict=False,
)
),
)


Expand Down Expand Up @@ -191,17 +196,17 @@ def ascii_histogram(seq) -> None:
# ---------------------------------------------------------------------

sns.set_style("darkgrid")
# Suppress the kwarg warning related to normed/density from Matplotlib.
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
sns.histplot(d, kde=True)
plt.title("Seaborn's histplot()")
plt.show()

sns.distplot(d)
plt.title("Seaborn's distplot()")
plt.show()
params = stats.laplace.fit(d)
x_laplace = np.linspace(d.min(), d.max(), num=250)

sns.distplot(d, fit=stats.laplace, kde=False)
plt.title("Histogram with Fitted Laplace Distribution")
plt.show()
ax = sns.histplot(d, stat="density")
ax.plot(x_laplace, stats.laplace.pdf(x_laplace, *params))
plt.title("Histogram with Fitted Laplace Distribution")
plt.show()


data = np.random.choice(
Expand Down
5 changes: 5 additions & 0 deletions histograms/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
matplotlib==3.11.2
numpy==2.5.3
pandas==3.0.6
scipy==1.18.1
seaborn==0.13.2
Loading