python代码:绘制85%,90%,95%的置信区间,在一张图上
时间: 2023-11-12 20:30:34 浏览: 184
好的,我可以帮你实现这个功能。我假设你已经有了数据和置信区间的计算结果,下面是一个绘制置信区间的 Python 代码示例:
```python
import matplotlib.pyplot as plt
import numpy as np
# 假设你有三组数据和对应的置信区间,分别存储在以下三个列表中
data1 = [10, 12, 15, 20, 25, 30, 35, 40, 45, 50]
conf_int1 = [(8, 12), (10, 14), (13, 17), (18, 22), (23, 27), (28, 32), (33, 37), (38, 42), (43, 47), (48, 52)]
data2 = [15, 18, 20, 22, 25, 28, 30, 32, 35, 38]
conf_int2 = [(12, 18), (15, 21), (17, 23), (19, 25), (22, 28), (25, 31), (27, 33), (29, 35), (32, 38), (35, 41)]
data3 = [8, 10, 12, 15, 18, 20, 22, 25, 28, 30]
conf_int3 = [(6, 10), (8, 12), (10, 14), (13, 17), (16, 20), (18, 22), (20, 24), (23, 27), (26, 30), (28, 32)]
# 设定置信区间的颜色和透明度
color = 'gray'
alpha = 0.5
# 绘制数据和置信区间的线条
plt.plot(data1, label='Data 1')
plt.fill_between(range(len(data1)), [x[0] for x in conf_int1], [x[1] for x in conf_int1], color=color, alpha=alpha)
plt.plot(data2, label='Data 2')
plt.fill_between(range(len(data2)), [x[0] for x in conf_int2], [x[1] for x in conf_int2], color=color, alpha=alpha)
plt.plot(data3, label='Data 3')
plt.fill_between(range(len(data3)), [x[0] for x in conf_int3], [x[1] for x in conf_int3], color=color, alpha=alpha)
# 添加图例和标签
plt.legend(loc='upper left')
plt.title('Confidence Intervals')
plt.xlabel('Data Points')
plt.ylabel('Value')
# 添加水平线,表示置信区间的边界
plt.axhline(np.percentile(data1, 5), linestyle='--', color='gray', alpha=0.5)
plt.axhline(np.percentile(data1, 95), linestyle='--', color='gray', alpha=0.5)
plt.axhline(np.percentile(data2, 5), linestyle='--', color='gray', alpha=0.5)
plt.axhline(np.percentile(data2, 95), linestyle='--', color='gray', alpha=0.5)
plt.axhline(np.percentile(data3, 5), linestyle='--', color='gray', alpha=0.5)
plt.axhline(np.percentile(data3, 95), linestyle='--', color='gray', alpha=0.5)
# 显示图形
plt.show()
```
这段代码可以绘制多组数据和对应的置信区间,以及表示置信区间边界的水平线。你可以根据自己的需要修改数据和置信区间的值,以及调整图形的样式和标签。
阅读全文