简体   繁体   中英

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:

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. Is it possible to change the order of the x-axis in the lineplot? I tried to use the "order" function within the sns.lineplot as follows:

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'

sort=False will do it.

As the seaborn doc states:

sort : boolean, optional

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.

The x variables are sorted in their "string-order":

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

which is not wanted.

The wanted behaviour is the aggregation by the x-values. This is done with the estimator='mean' (default). Every "Pulse Measure"(y) is grouped by the "Pulse Time" (x) and then the mean is calculated.

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

My Plot with other sample data:

x 变量的正确顺序

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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