【问题标题】:How to improve this seaborn countplot?如何改进这个seaborn countplot?
【发布时间】:2018-06-11 02:52:49
【问题描述】:

我使用以下代码在 python 中使用 seaborn 生成计数图:

sns.countplot( x='Genres', data=gn_s)

但我得到以下输出:

我无法清楚地看到 x 轴上的项目,因为它们是重叠的。我该如何纠正? 此外,我希望所有项目按数量递减顺序排列。我怎样才能做到这一点?

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    您可以使用选择x-axis 为垂直,例如:

    g = sns.countplot( x='Genres', data=gn_s)
    g.set_xticklabels(g.get_xticklabels(),rotation=90)
    

    或者,您也可以这样做:

    plt.xticks(rotation=90)
    

    【讨论】:

    • 我收到以下错误:set_xticklabels() 缺少 1 个必需的位置参数:'labels'
    • 谢谢我把标签翻了。关于如何按递增/递减顺序排列的任何提示?
    • 可以使用sns.countplot中的order参数,例如sns.countplot( x='Genres', data=gn_s, order = gn_s['Genres'].value_counts().index)
    • 你可以阅读seaborn.countplot parameters
    【解决方案2】:

    引入 matplotlib 以提前设置轴,以便您可以通过将轴刻度标签旋转 90 度和/或更改字体大小来修改它们。要按顺序排列示例,您需要修改源。我假设您是从 pandas 数据框开始的,例如:

    data = data.sort_values(by='Genres', ascending=False) labels = # list of labels in the correct order, probably your data.index fig, ax1 = plt.subplots(1,1) sns.countplot( x='Genres', data=gn_s, ax=ax1) ax1.set_xticklabels(labels, rotation=90)

    可能会有帮助。

    edit 接受来自 cmets 的 andrewnagyeb 的建议来订购情节:

    sns.countplot( x='Genres', data=gn_s, order = gn_s['Genres'].value_counts().index)

    【讨论】:

    • 感谢部分工作,标签垂直翻转。但我仍然没有按降序排列它们,因为它是一个计数图。它正在计算特定单词出现的次数并最终绘制结果。
    猜你喜欢
    • 2020-08-30
    • 2017-07-22
    • 2023-01-23
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多