离散型变量分布
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# 查看离散型随机变量的分布
arr1 = np.random.randint(1,10,(1000,))
# print(arr1)
df = pd.DataFrame(arr1, )
# (df[0].value_counts().sort_index()/df.shape[0]).plot() # 下面3行可以用这一行一步到位
data = df[0].value_counts().sort_index()/df.shape[0]
x = data.index
y = data.values
fig, ax = plt.subplots() # Create a figure containing a single axes.
ax.plot(x, y) # Plot some data on the axes.
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/0b9aec058d2889945203e9754396b6af.png)
查看连续型随机变量分布
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# 查看连续型随机变量的分布,可以绘制直方图
x = np.random.randn(10000) # 正太分布生成x
fig, ax = plt.subplots() # Create a figure containing a single axes.
ax.hist(x, bins=100) # Plot some data on the axes.
ax.set_xlabel('x') # x是这个取值
ax.set_ylabel('num') #
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/8cd2848290a0f65e15b6d436cbb3e1b2.png)
可以看到整体也是呈现正太分布的