Skip to content

Commit

Permalink
Script to plot a configurable binomial distribution
Browse files Browse the repository at this point in the history
This script is meant to produce plots to use in figures
  • Loading branch information
nathanpainchaud committed Nov 8, 2023
1 parent 495dbf1 commit 38337e5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions didactic/scripts/binomial_figure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def main():
"""Script that takes a number of trials and a probability of success and plots the binomial distribution.
Note: The plot of the binomial distribution is produced using ``seaborn.objects.Bars`` which is more appropriate
for continuous histograms.
"""
import argparse
from pathlib import Path

import numpy as np
import scipy.stats as stats
import seaborn.objects as so

parser = argparse.ArgumentParser(description="Plot a binomial distribution.")
parser.add_argument("--n", type=int, default=6, help="Number of trials.")
parser.add_argument("--p", type=float, default=0.4, help="Probability of success.")
parser.add_argument("--output_name", type=Path, default="binomial_distribution.svg", help="Output file name")
args = parser.parse_args()

# Compute the binomial distribution
x = np.arange(args.n)
y = stats.binom.pmf(x, args.n, args.p)

# Plot the binomial distribution
plot = so.Plot(x=x, y=y).add(so.Bars()).label(x="k", y="p(k)")
plot.save(args.output_name, bbox_inches="tight")


if __name__ == "__main__":
main()

0 comments on commit 38337e5

Please sign in to comment.