简体   繁体   English

如何更改 seaborn 线图中 x 轴标签的顺序?

[英]How to change the order of x-axis labels in a seaborn lineplot?

I have the following data frame:我有以下数据框:

df1_Relax_Pulse_Melted.head()

Task    Pulse Time  Pulse Measure
0   Language    PRE_RELAX_PULSE 90.0
1   Language    PRE_RELAX_PULSE 94.0
2   Language    PRE_RELAX_PULSE 52.0
3   Language    PRE_RELAX_PULSE 70.0
4   Language    PRE_RELAX_PULSE 84.0

When I attempt a barplot of this data, I get the following:当我尝试对此数据进行条形图时,我得到以下信息:

ax = sns.barplot(x="Pulse Time", y="Pulse Measure", hue="Task", data=df1_Relax_Pulse_Melted)

在此处输入图像描述

However, when I try to use a line plot, I get the following:但是,当我尝试使用 plot 行时,我得到以下信息:

ax = sns.lineplot(x="Pulse Time", y="Pulse Measure", hue="Task", data=df1_Relax_Pulse_Melted)

在此处输入图像描述

As can be seen in the image, the order of the x-axis labels is in a different order from the barplot.从图中可以看出,x 轴标签的顺序与条形图的顺序不同。 Is it possible to change the order of the x-axis in the lineplot?是否可以更改线图中 x 轴的顺序? I tried to use the "order" function within the sns.lineplot as follows:我尝试在 sns.lineplot 中使用“订单”function,如下所示:

ax = sns.lineplot(x="Pulse Time", y="Pulse Measure", hue="Task", data=df1_Relax_Pulse_Melted, order='PRE_RELAX_PULSE','30S_RELAX_PULSE','POST_RELAX_PULSE')

However, that produces an error.但是,这会产生错误。

``AttributeError: 'Line2D' object has no property 'order' ``AttributeError: 'Line2D' object 没有属性 'order'

sort=False will do it. sort=False会做到的。

As the seaborn doc states:正如seaborn 文档所述:

sort : boolean, optional排序:boolean,可选

If True, the data will be sorted by the x and y variables, otherwise lines will connect points in the order they appear in the dataset.如果为 True,则数据将按 x 和 y 变量排序,否则线条将按照它们在数据集中出现的顺序连接点。

The x variables are sorted in their "string-order": x 变量按其“字符串顺序”排序:

'30s_RELAX_PULSE' < 'POST_RELAX_PULSE' < 'PRE_RELAX_PULSE'

which is not wanted.这是不想要的。

The wanted behaviour is the aggregation by the x-values.想要的行为是 x 值的聚合。 This is done with the estimator='mean' (default).这是通过estimator='mean' (默认)完成的。 Every "Pulse Measure"(y) is grouped by the "Pulse Time" (x) and then the mean is calculated.每个“脉冲测量”(y) 按“脉冲时间”(x) 分组,然后计算平均值。

ax = sns.lineplot(x="Pulse Time", y="Pulse Measure", hue="Task",sort= False, data=df1_Relax_Pulse_Melted)

My Plot with other sample data:我的 Plot 和其他样本数据:

x 变量的正确顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM