在您的原始数据框中,缺少 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 月的数据仍然缺失,与原始数据相同。