两种方法,直方图的叠加

两种方法,直方图的叠加,一种用seaborn,一种用matplotlib.
示例如下:

import library

import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

generate some data for histgram

list1 = np.random.randint(100,size=1000)
list2 = np.random.randint(200,size=1000)

# also getthem into a dataframe into one column, but add a second column to label source
list = np.concatenate([list1,list2])
lables = ['l1']*len(list1)+['l2']*len(list2)
d = {'x':list,'label':lables}
df = pd.DataFrame(d)
display(df)
xlabel
049l1
193l1
238l1
396l1
432l1
.........
199518l2
1996102l2
19971l2
1998175l2
199977l2

2000 rows × 2 columns

method 1, overlay dataframe columns with different labels

sns.displot(df, x="x", hue="label", stat="density")

在这里插入图片描述


method 2, overlay two seperate lists into histograms using matplotlib

plt.figure(figsize=(8,6))
plt.hist(list1,bins=20, alpha=0.5, label="data1")
plt.hist(list2, bins=20, alpha=0.5, label="data2")

plt.xlabel("Data", size=14)
plt.ylabel("Count", size=14)

plt.title("Multiple Histograms with Matplotlib")
plt.legend(loc='upper right')

在这里插入图片描述
关注更多:
https://datasciencebyexample.com/2021/07/26/2021-07-26-1/
datascience by example
English Learning by Example

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值