数据可视化 - 多个直方图

 注意事项:

数据data后,自动统计data内部重复元素的个数作为直方图的y轴,从小到大的元素自动排列作为直方图的x轴

import os;import glob; import sys
import numpy as np
import scipy.ndimage as ndimage
from tqdm import tqdm, trange
import matplotlib.pyplot as plt

def multi_hist(data, title="hospital_patient", xlabel="hospitals", ylabel="patients", bins=16):
    # data : shape = M*N
   # assert len(np.shape(data))==2, r"数据要二维度的"
    title = r"Histogram with '{}' bins".format(title)
    xlabel = r"{}_({})".format(xlabel, "M")
    ylabel = r"{}_({})".format(ylabel, "M")
    num = len(data)
    label = [r"bar. - {}".format(str(i)) for i in range(num)]
    color = [j for i,j in zip(range(num), ['r', 'g', 'b', 'y'])]
    plt.hist(                 ## 绘制数据直方图
        x = data,             # 输入数据 - 自动统计相同元素出现的数量
        bins = bins,          # 指定直方图的条形数为20个
        edgecolor = 'w',      # 指定直方图的边框色
        color = color,        # 指定直方图的填充色
        label = label,        # 为直方图呈现图例
        density = False,      # 是否将纵轴设置为密度,即频率
        alpha = 0.8,          # 透明度
        rwidth = 1,           # 直方图宽度百分比:0-1
        stacked = True)       # 当有多个数据时,是否需要将直方图呈堆叠摆放,默认水平摆放
    # 显示图例
    plt.title(label=title)
    plt.xlabel(xlabel=xlabel)
    plt.ylabel(ylabel=ylabel)
    plt.legend()
    # 显示图形
    plt.show()
    pass

sample1 = [0,0,0,1,2,2,4,5,3,6,6,6,6,6,10,10,10,15,15]
sample2 = [0,0,1,2,3,4,5,6,7,8,9,9]
data = [sample1, sample2]
multi_hist(data, title="hospital_patient", xlabel="hospitals", ylabel="patients")        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智能之心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值