seaborn 绘制dataframe histogram 直方图

我们经常需要绘制直方图来可视化某些特征或变量的分布。
如何快速获得有用的情节并完成工作? 如果我们关心的是每个值的频率,seaborn 提供 一个方便的方法,countplot()函数,不用自己统计数据就可以得到图,迅速完成工作。

参考下面的示例:

get the data and do a count plot

%matplotlib inline
import seaborn as sns


titanic = sns.load_dataset("titanic")
titanic['class'] = titanic['class'].astype('str')
display(titanic)
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
003male22.0107.2500SThirdmanTrueNaNSouthamptonnoFalse
111female38.01071.2833CFirstwomanFalseCCherbourgyesFalse
213female26.0007.9250SThirdwomanFalseNaNSouthamptonyesTrue
311female35.01053.1000SFirstwomanFalseCSouthamptonyesFalse
403male35.0008.0500SThirdmanTrueNaNSouthamptonnoTrue
................................................
88602male27.00013.0000SSecondmanTrueNaNSouthamptonnoTrue
88711female19.00030.0000SFirstwomanFalseBSouthamptonyesTrue
88803femaleNaN1223.4500SThirdwomanFalseNaNSouthamptonnoFalse
88911male26.00030.0000CFirstmanTrueCCherbourgyesTrue
89003male32.0007.7500QThirdmanTrueNaNQueenstownnoTrue

891 rows × 15 columns

sns.set_theme(style="darkgrid")
ax = sns.countplot(x="embark_town", data=titanic)

在这里插入图片描述

what if we have too many values for the feature, and we can’t plot all of their distributions in the histogram?

# get the distinct values first, then choose the top n values we want to present; here we choose 2 as an example

sub_index = titanic['class'].value_counts().index[:2]
sub_data = titanic[titanic['class'].isin(sub_index)]
sub_data = sub_data.reset_index(drop=True)

ax = sns.countplot(x="class", data=sub_data)


在这里插入图片描述

# we can also explicitly require the order to be ascending
ax = sns.countplot(x="class", data=sub_data,order=sub_index[::-1])

在这里插入图片描述

now how to show the value counts for two categorical variables?

ax = sns.countplot(x="class", hue="who", data=titanic)

在这里插入图片描述

更多精彩文章:
https://datasciencebyexample.com/
datascience by example

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值