I'm using seaborn to plot values of time spent on an activity in function of the trainig session. The problem that I have is that since I have two distinct groups I've set the x values to for example 0.75 for group 1 and 1.25 for group 2 for session 1. I'm trying to get the xticks on the values 1, 2, 3... but I can't find a way to do it.
I've tried setting the xtick values to the numbers that go from 1 to 10 but that puts tick in the first 10 elements of my x values instead of the points 1 through 10 in the x axis:
plt.xticks(list(range(1,11)))
I'm using sns.stripplot()
to create the plot, and I have multiple subjects for each population, and I want the subjects from population 1 to have different colors in a scale of blues and subjects from population 2 to have different colors in a scale of reds, that's why I can't just use hue='population'
, because I'm using hue='subject
within each population.
Using native_scale=True
solved the problem, so thank you to the person that commented that!
I can think of two approaches to solve this. The first is a kind of manual adjustment where you create a new column, say x_vals
, that adjusts the x-values for each group to avoid overlap, then plotting the data with these adjusted x-values, and then customising the x-ticks using plt.xticks()
to align them with the original session values (however this last step likely needs matplotlib
, which together with it being a manual adjustment, makes this solution not very elegant or Pythonic.
Second, using native_scale=True
with sns.stripplot()
could be a more appropriate approach in large part due to its simplicity and alignment with native data scales. The potential downsides of this approach are that it does not provide inherent visual separation between different groups on the same x-tick. This can potentially be overcome by "jittering" the points (by using jitter=True
in stripplot()
, though in this case there could be an unwelcome interaction between jitter=
and dodge=
. Some experimentation is probably needed. Other possible downsides of this approach are limited control over placement of points, and while jittering may help, if the data are dense there will likely be overlapping points.