Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

enter image description here

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)

enter image description here

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'

like image 250
arkadiy Avatar asked Oct 07 '19 00:10

arkadiy


1 Answers

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:

correct order of the x variables

like image 73
tweini Avatar answered Nov 03 '22 15:11

tweini