如何在Matplotlib中导入数值与概率绘制累积分布函数及生存函数?
Cumulative Distribution & Survival Function Visualization
I built a plot that displays both the cumulative distribution and survival function for a dataset using NumPy and Matplotlib. Here's the full code implementation, plus a quick breakdown of what each part does:
import numpy as np import matplotlib.pyplot as plt # Calculate histogram bins and corresponding values from the dataset values, base = np.histogram(serWEB, bins=100) # Compute cumulative counts for the distribution cumulative = np.cumsum(values) # Plot the cumulative distribution function (CDF) in blue plt.plot(base[:-1], cumulative, c='blue') plt.title('Cumulative Distribution & Survival Function') plt.xlabel('X Data') plt.ylabel('Count') # Plot the survival function (total observations minus cumulative counts) in green plt.plot(base[:-1], len(serWEB)-cumulative, c='green') plt.show()
Key Observations:
- The blue line shows the cumulative distribution: it tracks how many observations fall at or below each value in the dataset.
- The green line represents the survival function, which tells you how many observations remain above each value (calculated by subtracting cumulative counts from the total number of entries in
serWEB). - This plot centers entirely on the numerical distribution of the data, making it straightforward to compare these two complementary metrics at a glance.
内容的提问来源于stack exchange,提问作者Mark Ginsburg
相关产品推荐
相关产品推荐

