【问题标题】:plot distributions (e.g. from bin counts) using seaborn distplot or similar seaborn functionality使用 seaborn distplot 或类似的 seaborn 功能绘制分布(例如从 bin 计数)
【发布时间】:2018-07-12 10:49:50
【问题描述】:

Seaborn 是一个很棒的软件包,countplot 是一个很棒的功能,具有令人愉悦的视觉效果。

有一个关于如何access-to-bin-counts-in-seaborn-distplot 的问题。例如。当前答案使用numpy.histogram

问题是,在我们获得分布并根据需要对其进行处理之后,如何使用更改后的分布来创建与计数图等效的图,例如

tl;dr:这是我的问题(示例取自 seaborn.countplot):

复制粘贴代码:

%matplotlib inline
import seaborn as sns
sns.set(style="darkgrid")
titanic = sns.load_dataset("titanic")
ax = sns.countplot(x="class", data=titanic)

import numpy as np
titanic_classes = titanic['class'].map({s:i for i,s in enumerate(titanic['class'].dtype.categories)}).values
titanic_counts = np.bincount(titanic_classes)
#now add ship2 counts
ship2_counts = [8, 9, 25]
total_counts = titanic_counts + ship2_counts
total_counts

【问题讨论】:

    标签: python matplotlib histogram seaborn


    【解决方案1】:

    您可以使用sns.barplot()

    sns.barplot(x = titanic["class"].dtype.categories, y = total_counts)
    

    【讨论】:

    • 谢谢!实际上,unique 给出了错误的顺序(按字母顺序),修复了使用 titanic["class"].dtype.categories 的答案。
    猜你喜欢
    • 2015-10-22
    • 2020-12-02
    • 2016-05-25
    • 2017-10-12
    • 2018-02-13
    • 2020-06-16
    • 2020-09-26
    • 2018-05-30
    相关资源
    最近更新 更多