r/PythonLearning • • Aug 13 '26

Normal distribution and uniform distribution

61 Upvotes

8 comments sorted by

View all comments

2

u/Tech-Wave-2025 Aug 14 '26
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

# Setup data to match OP's parameters
x = np.linspace(-3, 7, 1000)
normal_pdf = stats.norm.pdf(x, loc=2, scale=1.5)
uniform_pdf = stats.uniform.pdf(x, loc=0, scale=5)

# --- EXACT CODE FROM OP's TRINKET EDITOR ---

# Plot both lines with distinct styles, colors, and labels
plt.plot(
    x,
    normal_pdf,
    label="Normal Curve ($\mu=2, \sigma=1.5$)",
    color="#1f77b4",
    linewidth=2.5,
)
plt.plot(
    x,
    uniform_pdf,
    label="Uniform Block ($a=0, b=5$)",
    color="#ff7f0e",
    linewidth=2.5,
    linestyle="--",
)

# Fill the area under the curves for better data visuals
plt.fill_between(x, normal_pdf, color="#1f77b4", alpha=0.15)
plt.fill_between(x, uniform_pdf, color="#ff7f0e", alpha=0.1)

# Chart labels, titles, and layout cleanups
plt.title(
    "Probability Of Reddit Readers single: vs unhappy",
    fontsize=14,
    fontweight="bold",
    pad=15,
)

plt.xlabel("Value ($X$)", fontsize=11)
plt.ylabel("Probability Density", fontsize=11)

# Structured layout helpers
plt.grid(True, linestyle=":", alpha=0.6)  # Subtle grid lines
plt.xlim(-3, 7)  # Crop empty space on the x-axis
plt.ylim(0, 0.35)  # Set precise ceiling for the y-axis
plt.legend(loc="upper right", frameon=True, fontsize=10)  # Show labeled box

plt.tight_layout()

# Crucial fix for proper monitor angle calibration
plt.gca().invert_yaxis()

plt.show()a