【问题标题】:How do I fix missing bar from seaborn bar plot after changing the name of ticks?更改刻度名称后,如何修复 seaborn 条形图中缺少的条形?
【发布时间】:2021-10-29 16:36:53
【问题描述】:

我正在尝试使用 seaborn 绘制条形图,并且我想更改 xticks 的标签。 当刻度保持数字时,所有条形图都显示在图表上。为了获得这些数字,我创建了一个包含每个销售日期月份的列,并使用 grouby 按月份对数据进行分组。

pr_sales = sns.barplot(x='Month', y='dollartotal', color='lightblue', data=df_pr_2020,   )
pr_sales.set(xlabel='2020 by Month', ylabel='Dollars', title='2020 Pre-Roll Sales ')

参见下面的 X 轴编号:

但是当我将轴转换为标签时,我会丢失一条数据。这段代码的主要区别是月份取自saledate 列。

pr_sales = sns.barplot(x='Month', y='dollartotal', color='lightblue', data=df_pr_2020,   )
    
pr_sales.set(xlabel='2020 by Month', ylabel='Dollars', title='2020 Pre-Roll Sales ')
pr_sales.set_xticks(range(len(Months)))
    
pr_sales.set_xticklabels(Months)
    
Months =df_pr_2020['saledate'].dt.strftime('%b')

如您所见,现在显示的是 12 月:

【问题讨论】:

    标签: python pandas datetime matplotlib seaborn


    【解决方案1】:

    在您的原始数据框中,缺少 april 的数据,因此当您更改 tik 标签时,刻度向左移动,并且 december 似乎是空的。
    我想你正在使用这样的数据框:

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    
    samples = 10
    df_pr_2020 = pd.DataFrame({'date': np.repeat(pd.date_range(start = '2020-01-01', end = '2020-12-31', freq = 'D'), samples)})
    df_pr_2020 = df_pr_2020[df_pr_2020['date'].dt.month != 4]
    df_pr_2020['dollartotal'] = 14000*df_pr_2020['date'].dt.month + 150000 + 20000*np.random.randn(len(df_pr_2020))
    df_pr_2020['Month'] = df_pr_2020['date'].dt.month.astype(str).apply(lambda x: x.zfill(2))
    
               date    dollartotal Month
    0    2020-01-01  176160.921650    01
    1    2020-01-01  166157.143534    01
    2    2020-01-01  187411.157138    01
    3    2020-01-01  114761.306578    01
    4    2020-01-01  164297.114934    01
    5    2020-01-01  163810.407586    01
    6    2020-01-01  133359.064046    01
    7    2020-01-01  151415.390232    01
    8    2020-01-01  131282.857219    01
    9    2020-01-01  155171.540172    01
    10   2020-01-02  189664.518915    01
    11   2020-01-02  156596.250250    01
    12   2020-01-02  148865.016156    01
    13   2020-01-02  173507.621407    01
    14   2020-01-02  155717.433085    01
    15   2020-01-02  166862.159482    01
    

    当你绘制时(注意缺少四月):

    当您提取'Month' 列时,您已经可以将其从月份编号转换为月份短名称:

    df_pr_2020['Month'] = df_pr_2020['date'].dt.month_name().str[:3]
    

    完整代码

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    
    samples = 10
    df_pr_2020 = pd.DataFrame({'date': np.repeat(pd.date_range(start = '2020-01-01', end = '2020-12-31', freq = 'D'), samples)})
    df_pr_2020 = df_pr_2020[df_pr_2020['date'].dt.month != 4]
    df_pr_2020['dollartotal'] = 14000*df_pr_2020['date'].dt.month + 150000 + 20000*np.random.randn(len(df_pr_2020))
    df_pr_2020['Month'] = df_pr_2020['date'].dt.month_name().str[:3]
    
    
    pr_sales = sns.barplot(x='Month', y='dollartotal', color='lightblue', data=df_pr_2020)
    pr_sales.set(xlabel='2020 by Month', ylabel='Dollars', title='2020 Pre-Roll Sales ')
    
    plt.tight_layout()
    
    plt.show()
    

    如您所见,x 刻度已转换为月份短名称,而 4 月的数据仍然缺失,与原始数据相同。

    【讨论】:

    • 如果您不想修改原始数据框,可以将df_pr_2020['date'].dt.month_name().str[:3] 直接传递给x
    • @mwaskom 在这种情况下,df_pr_2020.date.dt.strftime('%b')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2021-10-29
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多