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

matplotlib绘制直方图报'Rectangle'无left属性错误如何修复

错误原因
  • 新版matplotlib.pyplot.bar函数已废弃left参数,条形的X轴坐标直接作为第一个位置参数传入,不需要单独通过left关键字参数指定,你代码中同时传入了第一个位置参数200和left参数,触发了属性不存在的报错。
  • 你传入的第一个位置参数200属于冗余无效参数,和你要绘制360个分组的直方图需求不匹配。
修复方法

将循环内的bar调用代码修改为如下即可解决报错:

matplotlib.pyplot.bar(numpy.arange(360), height=hist[0])
可选优化建议

HSV色彩空间的色相通道取值范围为[0,1],对应实际色相角度[0°,360°],你可以对直方图X轴做映射,提升结果可读性:

for idx, hsv_fruit_data in enumerate(hsv_fruits_data):
    fruit = fruits[idx]
    # 统计直方图同时获取分组边界
    hist, bins = numpy.histogram(a=hsv_fruit_data[:, :, 0], bins=360)
    # X轴映射为0-360度,设置柱宽为1避免空隙
    matplotlib.pyplot.bar(bins[:-1] * 360, height=hist, width=1)
    matplotlib.pyplot.xlabel("色相(°)")
    matplotlib.pyplot.ylabel("像素频数")
    matplotlib.pyplot.savefig(f"{fruit}-hue-histogram.jpg", bbox_inches="tight")
    matplotlib.pyplot.close("all")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 17:09:02