Возможно, вы ищете усеченное нормальное распределение .Использование scipy.stats.truncnorm
,
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
lower, upper = (0.0, 0.0), (1.0, 1.0)
mu, sigma = np.array([0.2, 0.2]), np.array([0.1, 0.05])
X = stats.truncnorm(
(lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
data = X.rvs((10000, 2))
fig, ax = plt.subplots()
ax.hist(data[:, 0], density=True, alpha=0.5, bins=20)
ax.hist(data[:, 1], density=True, alpha=0.5, bins=20)
plt.show()
дает
Вот еще один способ визуализациипример.Код в основном взят из галереи matplotlib :
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
lower, upper = (0.0, 0.0), (1.0, 1.0)
mu, sigma = np.array([0.2, 0.2]), np.array([0.1, 0.05])
X = stats.truncnorm(
(lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
data = X.rvs((10000, 2))
x, y = data.T
nullfmt = mticker.NullFormatter() # no labels
# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left + width + 0.02
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]
# start with a rectangular Figure
plt.figure(1, figsize=(8, 8))
axScatter = plt.axes(rect_scatter)
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)
# no labels
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)
# the scatter plot:
axScatter.scatter(x, y)
axScatter.set_xlim((-0.1, 0.7))
axScatter.set_ylim((-0.1, 0.5))
bins = 20
axHistx.hist(x, bins=bins)
axHisty.hist(y, bins=bins, orientation='horizontal')
axHistx.set_xlim(axScatter.get_xlim())
axHisty.set_ylim(axScatter.get_ylim())
plt.show()