You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python绘制一维数组并清晰展示25th、50th、75th百分位数

解决方案

你的问题源于直接用折线图绘制无序数组,折线会因数据点随机起伏导致无法清晰观察分布特征。以下是几种针对性的解决方案,同时可以直接获取并展示目标分位数:

1. 箱线图(推荐,直接展示分位数)

箱线图原生支持展示数据的25th、50th(中位数)、75th百分位数,还能直观呈现数据的离散程度和异常值:

import matplotlib.pyplot as plt
import numpy as np

x = [9.904725629894529e-06, 1.2634955055546016e-05, 1.5201896530925296e-05, 2.9845547032891773e-05, 3.49247275153175e-05, 3.4970289561897516e-05, 4.780302697326988e-05, 4.802399416803382e-05, 4.810681639355607e-05, 4.843798524234444e-05, 4.859635737375356e-05, 5.9152505855308846e-05, 5.9193022025283426e-05, 5.9363908803788945e-05, 5.9468671679496765e-05, 6.630286952713504e-05, 7.005851512076333e-05, 7.014916627667844e-05, 8.021695248316973e-05, 8.989680645754561e-05, 9.008277265820652e-05, 9.028125350596383e-05, 9.037737618200481e-05, 9.04681728570722e-05, 9.083149052457884e-05, 0.00021005164308007807, 0.00021028853370808065, 0.00021039179409854114, 0.00021039319108240306, 0.00021107416250742972, 0.00021134318376425654, 0.00021135005226824433, 0.00021196113084442914, 0.00021199103503022343, 0.0002120915160048753, 0.0002126666222466156, 0.000213045728742145, 0.00021316968195606023, 0.00021321520034689456, 0.00021339277736842632, 0.00021368247689679265, 0.0002137374976882711, 0.00021400606783572584, 0.00021451167413033545, 0.00021501131413970143]

# 计算分位数
q25, q50, q75 = np.percentile(x, [25, 50, 75])

plt.figure(figsize=(8,4))
plt.boxplot(x, vert=False)  # 横向箱线图更适合观察数值分布
plt.title('Data Distribution with Percentiles')
plt.xlabel('Value')

# 标注分位数
plt.text(q25, 1.1, f'25th: {q25:.8f}', ha='center')
plt.text(q50, 1.1, f'50th: {q50:.8f}', ha='center')
plt.text(q75, 1.1, f'75th: {q75:.8f}', ha='center')

plt.show()

2. 排序后绘制折线图

先对数组排序,再绘制折线,能清晰看到数据的递增趋势,同时标注分位数位置:

import matplotlib.pyplot as plt
import numpy as np

x_sorted = sorted(x)
q25, q50, q75 = np.percentile(x_sorted, [25, 50, 75])

plt.figure(figsize=(10,4))
plt.plot(x_sorted, marker='o', markersize=4, linestyle='-')
plt.title('Sorted Data with Percentiles')
plt.ylabel('Value')
plt.xlabel('Index')

# 绘制分位数水平线并标注
plt.axhline(y=q25, color='r', linestyle='--', label=f'25th: {q25:.8f}')
plt.axhline(y=q50, color='g', linestyle='--', label=f'50th: {q50:.8f}')
plt.axhline(y=q75, color='b', linestyle='--', label=f'75th: {q75:.8f}')

plt.legend()
plt.show()

3. 直方图+分位数竖线

用直方图展示数据的频率分布,叠加分位数竖线,直观看到分位数在分布中的位置:

import matplotlib.pyplot as plt
import numpy as np

q25, q50, q75 = np.percentile(x, [25, 50, 75])

plt.figure(figsize=(10,4))
n, bins, patches = plt.hist(x, bins=10, edgecolor='black')
plt.title('Data Histogram with Percentiles')
plt.xlabel('Value')
plt.ylabel('Frequency')

# 绘制分位数竖线
plt.axvline(x=q25, color='r', linestyle='--', label=f'25th: {q25:.8f}')
plt.axvline(x=q50, color='g', linestyle='--', label=f'50th: {q50:.8f}')
plt.axvline(x=q75, color='b', linestyle='--', label=f'75th: {q75:.8f}')

plt.legend()
plt.show()

内容的提问来源于stack exchange,提问作者sonia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 21:24:54