Below is a small, self-contained Python script that
- loads
sliding_scores-10-pos.npy,
- plots the data as a histogram, and
- saves the figure as
sliding_scores_hist.png (and/or shows it on-screen).
#!/usr/bin/env python3
"""
hist_sliding_scores.py
Plot the contents of `sliding_scores-10-pos.npy` as a histogram.
"""
import argparse
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
def main(npy_file: Path, bins: int, save_only: bool):
# ----- Load data --------------------------------------------------------
try:
scores = np.load(npy_file)
except FileNotFoundError as e:
raise SystemExit(f"File not found: {npy_file}\n{e}")
# ----- Plot -------------------------------------------------------------
plt.style.use("seaborn-v0_8-whitegrid")
fig, ax = plt.subplots(figsize=(8, 5))
ax.hist(
scores.ravel(), # flatten in case it isn't 1-D
bins=bins,
color="#3182bd",
edgecolor="black",
alpha=0.8,
)
ax.set_title(f"Histogram of {npy_file.name}")
ax.set_xlabel("Score")
ax.set_ylabel("Frequency")
fig.tight_layout()
# ----- Save / show ------------------------------------------------------
out_file = npy_file.with_suffix(".png")
fig.savefig(out_file, dpi=300)
print(f"Histogram saved to {out_file.resolve()}")
if not save_only:
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Plot a histogram of sliding-window scores stored in a .npy file."
)
parser.add_argument(
"-f",
"--file",
type=Path,
default=Path("sliding_scores-10-pos.npy"),
help="Path to the .npy file (default: sliding_scores-10-pos.npy)",
)
parser.add_argument(
"-b",
"--bins",
type=int,
default=50,
help="Number of histogram bins (default: 50)",
)
parser.add_argument(
"--save-only",
action="store_true",
help="Save the figure but do not open an interactive window",
)
args = parser.parse_args()
main(args.file, args.bins, args.save_only)
How to run (in the same directory as the .npy file):
python hist_sliding_scores.py # uses default bins=50 and shows the plot
python hist_sliding_scores.py -b 100 # specify a different number of bins
python hist_sliding_scores.py --save-only # just save PNG, don’t open a window
✨ Tip: If you’re working in a Jupyter notebook, you can skip the CLI part and just drop the core couple of lines:
import numpy as np
import matplotlib.pyplot as plt
scores = np.load("sliding_scores-10-pos.npy")
plt.hist(scores.ravel(), bins=50, color="steelblue", edgecolor="black")
plt.title("Histogram of sliding_scores-10-pos")
plt.xlabel("Score")
plt.ylabel("Frequency")
plt.show()