【问题标题】:Seaborn: Replace Bool with yes/no in the bar label for catplot of kind='count'Seaborn:在种类='count'的catplot的条形标签中将Bool替换为是/否
【发布时间】:2021-09-25 20:20:11
【问题描述】:

我有一张在 Seaborn 中制作的图表(catplot type=count),我试图通过将 True 和 False 替换为 Yes 和 No 来使它看起来更好看。

这是目前的代码:

g_q1 = sns.catplot(x='Q1', kind='count', order=[True, False], data=clean_df, height=4, aspect=1)
g_q1.set(xlabel='Question will be here', ylabel='Number of Participants')

对数据集包含的问题的回复都是布尔值,但我想用是和否(这将是受访者点击的实际答案)而不是真/假来标记条形。实现这一目标的最佳方法是什么?

现在它看起来像这样: Image of the resulting graph

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    你需要使用seaborn.FacetGridset_xticklabels方法:

    import pandas as pd
    import seaborn as sns
    
    from matplotlib import pyplot as plt
    
    clean_df = pd.DataFrame([True] * 30 + [False] * 20)
    g_q1 = sns.catplot(x=0, kind='count', order=[True, False], data=clean_df, height=4, aspect=1)
    g_q1.set(xlabel='Question will be here', ylabel='Number of Participants')
    g_q1.set_xticklabels(labels=['Yes', 'No'])
    
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 2019-08-30
      • 1970-01-01
      • 2019-04-01
      • 2021-06-18
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多