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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With