1

I am searching for a way to adjust the space between data points (red arrows) and between the x-ticks (green arrows) on a seaborn strip- or swarm-plot.

seaborn stripplot with given dataset

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = {'Days': np.full((48, 5), [4,7, 8, 9, 10]).reshape(-1),
        'Group': np.full((80, 3), ["Group1", "Group2", "Group3"]).reshape(-1), 
        'Value': np.random.rand(240)}
df = pd.DataFrame(data=data)
fig, ax = plt.subplots(figsize=(20, 10), dpi=80)
sns.stripplot(x=df.Days, y=df.Value, jitter=0, size=5, ax=ax, linewidth=1,
              dodge=True, hue=df.Group, palette="Set1", data=df)
plt.show()
Carlos Azevedo
  • 660
  • 3
  • 13
Broxy
  • 90
  • 7
  • Make the figure wider – Trenton McKinney Sep 11 '22 at 21:37
  • If I simply make the figure wider, the relative distance between the data points of the different x values does not change. My aim is to move the data points belonging to an x tick closer together so that it is clear which data points belong to which x value. This only works if one of the two distances (red arrow) or (green arrow) changes. – Broxy Sep 12 '22 at 10:21
  • Use `sns.swarmplot` instead: `sns.swarmplot(x='Days', y='Value', size=5, ax=ax, linewidth=1, hue='Group', palette="Set1", data=df)` – Trenton McKinney Sep 12 '22 at 14:14
  • As soon as I use a swarm plot and dodge=False, I lose the property that the data points line up under each other, which is not desired. Using a swarm plot is actually in this case more sensible and the overview of the groups is actually better, but the distance of the vertical line (red, blue, green) is still not adjustable. – Broxy Sep 12 '22 at 15:48

1 Answers1

0

The strip plot provides functionality called jitter which makes it an advantage to visualize the collision of data as shown in the image below. However, to adjust the space you should adjust the jitter in the strip plot method to a number greater than zero. you can make it 5 for example or any number that is appropriate for u.

sns.stripplot(x=df.Days, y=df.Value, jitter=5, data=df)

Moreover, u can see this video to understand more about strip plot https://www.youtube.com/watch?v=wNgxdH02hrw

see the image

  • Additionally, u can use a swarm plot instead of a strip plot, but make sure that your data is not (small and overlapped)! because this will cause a problem and it will not run. However, the code will be like this one. sns.swarmplot(x=df.Days, y=df.Value, data=df) – Abdozargina Sep 12 '22 at 14:22
  • As soon as I use jitter, the average distance between the individual data points does not change. In fact, the plot becomes even less clear, the individual groups (dodge = True) become unrecognizable and it is no longer clear which data point belongs to which x value. If we imagine the individual data points as a vertical line, I try to move them closer together. I try to move the individual groups (red, blue, green) of the x values closer together without destroying the vertical line of the groups so that it is clear which line belongs to which x value. – Broxy Sep 12 '22 at 15:42