在 Python 中,可以使用 NumPy 库的 random 模块生成服从线性分布的随机数,并使用 Matplotlib 库的 pyplot 模块绘制概率密度函数 (PDF) 的图像。

以下是一个示例代码:

import numpy as np
import matplotlib.pyplot as plt

# 生成服从线性分布的随机数
size = 1000
low = 0
high = 10
data = np.random.uniform(low, high, size)

# 绘制概率密度函数的图像
plt.hist(data, bins='auto', density=True, alpha=0.7, rwidth=0.85)
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.title('Linear Distribution PDF')
plt.show()

在上述示例中,我们使用 np.random.uniform 函数生成了 1000 个在 [0, 10) 范围内的随机数。然后,使用 plt.hist 函数绘制了这些随机数的概率密度函数图像。其中,参数 bins='auto' 表示自动选择合适的柱子数量,density=True 表示绘制的是概率密度函数而非直方图,alpha=0.7 表示图像的透明度为 0.7,rwidth=0.85 表示柱子的宽度为 0.85。

运行以上代码,将会显示一个线性分布的概率密度函数图像。


原文地址: https://www.cveoy.top/t/topic/o4Hb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录