pyplot画list数据的直方图

29 篇文章 3 订阅
21 篇文章 0 订阅
    from matplotlib import pyplot as plt

    # 参数依次为list,抬头,X轴标签,Y轴标签,XY轴的范围
    def draw_hist(myList, Title, Xlabel, Ylabel, Xmin, Xmax, Ymin, Ymax):
        plt.hist(myList, 50) # bins = 50,顺便可以控制bin宽度
        plt.xlim(Xmin, Xmax)
        plt.ylim(Ymin, Ymax)
        plt.xlabel(Xlabel)  # 横轴名
        plt.ylabel(Ylabel)  # 纵轴名

        plt.title(Title)
        plt.show()
draw_hist(area_list, 'Area Statistics of Different Images', 'Area', 'number', 0, 6000, 0.0, 3)  # 直方图展示

 --------------------------------------------------------------------------------------------------------------------------------------------

画 list[list]数据类型的hist

    # 统计预测结果图像中所有mask面积
    def area_statistics(pred):
        pred_conts = get_cnts(pred)
        area_list = []
        for c_num in range(len(pred_conts)):
            area = cv2.contourArea(pred_conts[c_num])
            area_list.append(area)
        return area_list

    area_list = self.area_statistics(pred_img)
    all_img_area_list.append(area_list)  
    self.draw_hist(all_img_area_list, 'Area Statistics of Different Images', 'Area', 'number', 0, 6000, 0.0, 3)  # 直方图展示

效果如下:

同一张图像上的不同区域的面积,用相同的颜色表示。

要使用Python绘制直方图,你可以使用matplotlib库中的plt.hist()函数。该函数的参数包括x(数据)、bins(区间数量)、range(取值范围)、density(是否归一化)、color(颜色)等等。下面是一个绘制直方图的示例代码: ```python import matplotlib.pyplot as plt # 数据 x = \[1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 1, 2, 1\] # 绘制直方图 plt.hist(x, bins=5, range=(1, 5), density=False, color='blue') # 设置标题和坐标轴标签 plt.title("Histogram") plt.xlabel("Value") plt.ylabel("Frequency") # 显示图形 plt.show() ``` 这段代码将数据x绘制成了一个直方图,设置了5个区间(bins),取值范围为1到5,不进行归一化,颜色为蓝色。你可以根据自己的数据和需求进行相应的修改。 #### 引用[.reference_title] - *1* *3* [python:绘制直方图(Histogram)](https://blog.csdn.net/kaever/article/details/105875929)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [超详细的Python matplotlib 绘制直方图 赶紧收藏](https://blog.csdn.net/weixin_54556126/article/details/121397129)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值