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

使用Matplotlib绘制指定区间(bins)和高度的柱状图

自定义区间与高度的柱状图绘制方案

先整理你的数据,提取出关键参数:

  • 区间左边界:[0.0, 0.0905, 0.1811, 0.2716, 0.3621, 0.4527, 0.5432, 0.6337, 0.7243, 0.8148, 0.9053, 0.9958]
  • 区间右边界:[0.0905, 0.1811, 0.2716, 0.3621, 0.4527, 0.5432, 0.6337, 0.7243, 0.8148, 0.9053, 0.9958, 1.0]
  • 对应高度:[0.02601, 0.13678, 0.22647, 0.31481, 0.40681, 0.50200, 0.58746, 0.68153, 0.76208, 0.86030, 0.95027, 0.99584]

用Python的matplotlib库即可实现匹配要求的柱状图,代码如下:

import matplotlib.pyplot as plt

# 定义数据
left_bounds = [0.0, 0.0905, 0.1811, 0.2716, 0.3621, 0.4527, 0.5432, 0.6337, 0.7243, 0.8148, 0.9053, 0.9958]
right_bounds = [0.0905, 0.1811, 0.2716, 0.3621, 0.4527, 0.5432, 0.6337, 0.7243, 0.8148, 0.9053, 0.9958, 1.0]
heights = [0.02601, 0.13678, 0.22647, 0.31481, 0.40681, 0.50200, 0.58746, 0.68153, 0.76208, 0.86030, 0.95027, 0.99584]

# 计算每个区间的宽度
widths = [r - l for l, r in zip(left_bounds, right_bounds)]

# 创建绘图画布
plt.figure(figsize=(10, 6))

# 绘制柱状图,align='edge'确保柱子左边界与区间左边界对齐
plt.bar(left_bounds, heights, width=widths, align='edge', edgecolor='black')

# 设置x轴刻度为所有区间边界,清晰展示区间范围
plt.xticks(left_bounds + [right_bounds[-1]])

# 添加轴标签与标题
plt.xlabel('区间范围')
plt.ylabel('高度')
plt.title('自定义区间高度柱状图')

# 显示y轴网格(可选)
plt.grid(axis='y', linestyle='--', alpha=0.7)

# 调整布局并展示图像
plt.tight_layout()
plt.show()

关键说明:

  • align='edge' 参数保证每个柱子的左边缘完全匹配你给定的区间左边界,柱子宽度等于区间实际长度,完美贴合需求中的Range范围。
  • 可以根据需求调整画布尺寸、柱子颜色、网格样式等细节。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 18:15:58