在海洋散点图中对分类x轴进行排序 [英] Sort categorical x-axis in a seaborn scatter plot

查看:38
本文介绍了在海洋散点图中对分类x轴进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用如下图所示的海洋散点图来绘制数据框中前30%的值.

同一情节的可复制代码:

导入 seaborn 为 snsdf = sns.load_dataset('iris')#function可返回数据帧中前30%的值.def extract_top(df):n =整数(0.3 * len(df))top = df.sort_values('sepal_length',升序= False).head(n)返回顶部#存储最高值顶部= extract_top(df)#绘图sns.scatterplot(data = top,x='物种', y='sepal_length',颜色 = '黑色',s = 100,标记 = 'x',)

在这里,我想按 order = ['virginica','setosa','versicolor'] 对x轴进行排序.当我尝试使用 order 作为 sns.scatterplot() 中的参数之一时,它返回了一个错误 AttributeError: 'PathCollection' object has no property 'order'.正确的做法是什么?

请注意:在数据框中, setosa 也是 species 中的类别,但是,在前30%的值中,其值均未下降.因此,该标签未显示在顶部可重现代码的示例输出中.但是我也希望按给定的顺序在x轴上显示该标签,如下所示:

解决方案

scatterplot() 不是该工作的正确工具.由于您有一个分类轴,因此您想使用 stripplot() 而不是 scatterplot().在此处

I am trying to plot the top 30 percent values in a data frame using a seaborn scatter plot as shown below.

The reproducible code for the same plot:

import seaborn as sns

df = sns.load_dataset('iris')

#function to return top 30 percent values in a dataframe.
def extract_top(df):
    n = int(0.3*len(df))
    top = df.sort_values('sepal_length', ascending = False).head(n)

    return top

#storing the top values
top = extract_top(df)

#plotting
sns.scatterplot(data = top,
                x='species', y='sepal_length', 
                color = 'black',
                s = 100,
                marker = 'x',)

Here, I want sort the x-axis in order = ['virginica','setosa','versicolor']. When I tried to use order as one of the parameter in sns.scatterplot(), it returned an error AttributeError: 'PathCollection' object has no property 'order'. What is the right way to do it?

Please note: In the dataframe, setosa is also a category in species, however, in the top 30% values non of its value is falling. Hence, that label is not shown in the example output from the reproducible code at the top. But I want even that label in the x-axis as well in the given order as shown below:

解决方案

scatterplot() is not the correct tool for the job. Since you have a categorical axis you want to use stripplot() and not scatterplot(). See the difference between relational and categorical plots here https://seaborn.pydata.org/api.html

sns.stripplot(data = top,
              x='species', y='sepal_length', 
              order = ['virginica','setosa','versicolor'],
              color = 'black', jitter=False)

这篇关于在海洋散点图中对分类x轴进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆