首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在seaborn.relplot中设置行顺序

无法在seaborn.relplot中设置行顺序
EN

Stack Overflow用户
提问于 2020-02-21 10:06:20
回答 2查看 2.3K关注 0票数 1

我对python和seaborn很陌生,我正在使用来自卡格尔的数据集,现在我想将数据可视化,这是我的代码

代码语言:javascript
复制
def add_if_zero(x):
    if (x['stays_in_weekend_nights']+x['stays_in_week_nights'])==0:
        return 1
    else:
        return (x['stays_in_weekend_nights']+x['stays_in_week_nights'])

_data['total_days_of_stay']=_data.apply(lambda x:add_if_zero(x),axis=1)
x_order=['January','February','March','April','May','May','May','August','September','October','November','December']

sns.relplot(x='arrival_date_month',
            y='total_days_of_stay',hue='hotel',
            kind='line',data=_data,height=10,aspect=.9,row_order=x_order)

我得到了图,我希望命令是从一月到十二月,但是我在x轴上得到了一个随机变量。

我试着以数组和字符串格式将顺序传递给row_order属性,但是似乎没有什么效果。

根据文档

字符串的row_order、col_orderlists、组织网格行和/或列的可选顺序,否则顺序将从数据对象中推断出来。

更新1:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-15 23:18:06

使用sns.catplot,将所需的顺序作为字符串列表传递给函数,并设置kind='point',如下所示:

代码语言:javascript
复制
sns.catplot(... ,  
            kind='point', 
            order=['Jan', 'Feb' , ...., 'Dec'])
票数 1
EN

Stack Overflow用户

发布于 2020-02-23 22:19:35

您可能需要使用返回matplotlib lineplot()对象的Axes,这很容易配置。这里的基本思想是先绘制数字顺序int,然后添加标签。请考虑以下示例,该示例使用来自海运的示例数据集。

代码语言:javascript
复制
import calendar
import seaborn as sns
import matplotlib.pyplot as plt

# sample dataset
df = sns.load_dataset('flights')

# map month name with numerical order
d = dict((v,k) for k,v in enumerate(calendar.month_name[1:], start=1))
df['month_num'] = df.month.map(d)

# plot
fig, ax = plt.subplots(figsize=(10, 5))
sns.lineplot(x='month_num', y='passengers', data=df, hue='year', ax=ax)
# set xticks position and labels
ax.set_xticks(range(1, len(d)+1))
ax.set_xticklabels(d.keys(), rotation=30)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60336304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档