创建箱线图时使用月份名称而不是数字[重复]

问题描述 投票:0回答:1

我有什么

  1. 包含公司数据的数据框。尤其:
    1. 'Month Joined'
      作为 int64

    2. 'Years To Join'
      作为 int64

  2. 列表
    month_order

    ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

我想要什么

我想创建一个箱线图并将

xticks
从列表中的数字更改为月份名称。我使用这个代码:

sns.boxplot(
    x = companies['Month Joined'],
    y = companies['Years To Join'],
    showfliers = False,
    order = month_order
    )

却什么也没得到:

Empty boxplot

但是当我删除

order = month_order
时,一切正常(除了月份的名称是数字)

Correct boxplot but without months names

想要的结果

Desired result

问题

如何解决这个问题并获得想要的结果?

python seaborn boxplot
1个回答
0
投票

使用 set_xticks()

尝试一下
fig, ax = plt.subplots()
sns.boxplot(
    x = companies['Month Joined'],
    y = companies['Years To Join'],
    showfliers = False,
    order = month_order,
    ax=ax
)

ax.set_xticks(month_order)
© www.soinside.com 2019 - 2024. All rights reserved.