-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlot_number_distribution.py
More file actions
73 lines (63 loc) · 2 KB
/
Plot_number_distribution.py
File metadata and controls
73 lines (63 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Note: Seaborn library is used for plotting. Install it using:
# pip install seaborn
# Set the size of the sample
size = 10000
# Set the random seed for reproducibility
np.random.seed(42)
# Generate random numbers from a uniform distribution (0 to 1)
x1 = np.random.rand(size)
# Plot histogram with kernel density estimate (KDE) and statistical elements
plt.figure()
sns.histplot(
x1,
fill=True,
alpha=0.2,
bins=20,
kde=True,
element="step",
)
plt.axvline(x=x1.std(), linestyle="-.", color="green", label="std = {:.3f}".format(x1.std()))
plt.axvline(x=x1.mean(), linestyle="-.", color="red", label="mean = {:.3f}".format(x1.mean()))
plt.title("Uniform Distribution - Original")
plt.legend()
# plt.grid() # Uncomment this line if you want to include grid lines
plt.show()
# Plot histogram with KDE and independent density normalization
plt.figure()
sns.histplot(
x1,
fill=True,
alpha=0.2,
bins=20,
kde=True,
element="step",
stat="density",
)
plt.axvline(x=x1.std(), linestyle="-.", color="green", label="std = {:.3f}".format(x1.std()))
plt.axvline(x=x1.mean(), linestyle="-.", color="red", label="mean = {:.3f}".format(x1.mean()))
plt.title("Uniform Distribution - With Density Normalization")
plt.legend()
plt.show()
# Generate random numbers from a uniform distribution in the range [-1, 1]
a, b = -1.0, 1.0
np.random.seed(42)
x2 = np.random.uniform(low=a, high=b, size=size)
# Plot histogram with KDE, step filling, and no common normalization
plt.figure()
sns.histplot(
x2,
fill=True,
alpha=0.2,
bins=20,
kde=True,
element="step",
common_norm=False,
)
plt.axvline(x=x2.std(), linestyle="-.", color="green", label="std = {:.3f}".format(x2.std()))
plt.axvline(x=x2.mean(), linestyle="-.", color="red", label="mean = {:.3f}".format(x2.mean()))
plt.title("Uniform Distribution - Ranged")
plt.legend()
plt.show()