按月在 seaborn 小提琴情节中设置 x 范围

set x range in seaborne violinplot by month

我正在尝试使用 seaborn violinplot 绘制一年中记录的温度数据。

我设法完成了以下情节:

即使 2021 年的数据收集从 3 月开始,我也想在 1 月开始绘图,以便在彼此之上显示年度绘图,并能够直观地比较不同年份的相同月份。

我尝试了 set_xlim 的以下用法,但没有用

fig, ax = plt.subplots(figsize=(24,10))
fontsize=18
ax.set_xlim(1,12)
plt.rc('xtick', labelsize=fontsize) 
plt.rc('ytick', labelsize=fontsize) 

plt.rcParams['font.size']=24

seaborn.violinplot(x = weather21_2h.index.month, y = weather21_2h['T out_x'], ax=ax, palette="GnBu",inner = "box")
plt.title('Statistique des températures par mois 2021')
plt.xlabel('Mois', fontsize=24)
plt.ylabel('Températures (°C)')

plt.grid(True)

plt.show()

有办法吗?

您可以简单地将 order 参数传递给 saeborn.violinplot:

fig, ax = plt.subplots(figsize=(24,10))
fontsize=18

plt.rc('xtick', labelsize=fontsize)
plt.rc('ytick', labelsize=fontsize)

plt.rcParams['font.size']=24

sns.violinplot(x = weather21_2h.index.month,
               y = weather21_2h['T out_x'],
               ax=ax,
               palette="GnBu",
               inner = "box",
               order = range(1, 13))

ax.set_title('Statistique des températures par mois 2021')
ax.set_xlabel('Mois', fontsize=24)
ax.set_ylabel('Températures (°C)')

ax.grid(True)

plt.show()