Matplotlib Dot Plot With Two Categorical Variables
I would like to produce a specific type of visualization, consisting of a rather simple dot plot but with a twist: both of the axes are categorical variables (i.e. ordinal or non-n
Solution 1:
you could first convert time
and sex
to categorical type and tweak it a little bit:
df.sex = pd.Categorical(df.sex)
df.time = pd.Categorical(df.time)
axes = sns.scatterplot(x=df.time.cat.codes+np.random.uniform(-0.1,0.1, len(df)),
y=df.sex.cat.codes+np.random.uniform(-0.1,0.1, len(df)),
size=df.tip)
Output:
With that idea, you can modify the offsets (np.random
) in the above code to the respective distance. For example:
# groupinggroups = df.groupby(['time', 'sex'])
# compute the number of samples per groupnum_samples = groups.tip.transform('size')
# enumerate the samples within a groupsample_ranks = df.groupby(['time']).cumcount() * (2*np.pi) / num_samples
# compute the offsetx_offsets = np.where(num_samples.eq(1), 0, np.cos(df.sample_rank) * 0.03)
y_offsets = np.where(num_samples.eq(1), 0, np.sin(df.sample_rank) * 0.03)
# plotaxes = sns.scatterplot(x=df.time.cat.codes + x_offsets,
y=df.sex.cat.codes + y_offsets,
size=df.tip)
Output:
Post a Comment for "Matplotlib Dot Plot With Two Categorical Variables"