matplotlib

Matplotlib怎么绘制PDF和CDF的对比图

小亿
83
2024-05-21 12:40:23
栏目: 编程语言

要绘制PDF和CDF的对比图,可以使用Matplotlib库中的hist函数来绘制PDF,使用numpy库中的cumsum函数来计算CDF,然后使用Matplotlib中的plot函数来绘制CDF。

以下是一个示例代码,演示如何绘制PDF和CDF的对比图:

import numpy as np
import matplotlib.pyplot as plt

# 生成一些随机数据
data = np.random.normal(0, 1, 1000)

# 绘制PDF
plt.hist(data, bins=30, density=True, alpha=0.5, color='b')

# 计算CDF
counts, bin_edges = np.histogram(data, bins=30, density=True)
cdf = np.cumsum(counts)

# 绘制CDF
plt.plot(bin_edges[1:], cdf, color='r')

plt.xlabel('Value')
plt.ylabel('Probability')
plt.legend(['CDF', 'PDF'])
plt.show()

运行此代码将生成一个包含PDF和CDF的对比图,其中PDF用蓝色直方图表示,CDF用红色线图表示。您可以根据需要自定义图形的样式和参数。

0
看了该问题的人还看了