【问题标题】:What is Matplotlib's alternative for countplot from seaborn?Matplotlib 对 seaborn 的 countplot 的替代方案是什么?
【发布时间】:2019-04-13 15:42:04
【问题描述】:

我有以下数据:

male      843
female    466
Name: Sex, dtype: int64

我使用来自seaborncountplot 绘制了相同的条形图,它起作用了。

但我想知道matplotlib 中的替代方案。

我做到了:

sns.countplot(x = 'Sex', data = complete_data)

它给了我:

【问题讨论】:

    标签: python-3.x matplotlib seaborn


    【解决方案1】:

    假设你有这些数据:

    import numpy as np; np.random.seed(42)
    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({"Sex" : np.random.choice(["male",  "female"], size=1310, p=[.65, .35]),
                       "other" : np.random.randint(0,80, size=1310)})
    

    你可以在 seaborn 中绘制一个计数图

    import seaborn as sns
    sns.countplot(x="Sex", data=df)
    plt.show()
    

    或者您可以在 pandas 中创建条形图

    df["Sex"].value_counts().plot.bar()
    plt.show()
    

    或者您可以在 matplotlib 中创建条形图

    counts = df["Sex"].value_counts()
    plt.bar(counts.index, counts.values)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2021-11-13
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多