3

When I was plotting with Seaborn pointplot, the gap between two adjacent values on the x-axis should be different, but now all of them are same, how could I change that?

For example, the gap between 5 and 7 is two, the other gaps are 1, but in the plot they all have the same length.

import seaborn as sns
tips = sns.load_dataset('tips')
tips.loc[(tips['size'] == 6), 'size'] = 7
sns.pointplot('size', 'total_bill', 'sex', tips, dodge=True)

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
emma qqq
  • 77
  • 1
  • 6
  • 2
    In a point plot the xaxis is a categorical axes, imagine it to be more like ["apples", "bananas", "cherry"], hence there is no "distance". Instead, you would probably want to use a matplotlib scatter or errorbar plot. – ImportanceOfBeingErnest Oct 24 '17 at 09:40

2 Answers2

1

This is not possible in Seaborn, the reason seems to be that it would interfere with the nested grouping from the hue= parameter, as indicated to in the changelog describing why this functionality was removed from the sns.violinplot, which used to have a positions= keyword (as do plt.boxplot).

As indicated in the comments, it is possible to achieve the desired outcome using plt.errorbar.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

tips = sns.load_dataset('tips')
tips.loc[(tips['size'] == 6), 'size'] = 7

# This is to avoid that the points from the different curves are in the
# exact same x-position (so that the error bars to not obfuscate each other)
offset_amount = 0.1
gender_groups = tips.groupby('sex')
num_groups = gender_groups.ngroups
lims =  num_groups * offset_amount / 2
offsets = np.linspace(-lims, lims, num_groups) 

# Calculate and plot the mean and error estimates.
fig, ax = plt.subplots()
for offset, (gender_name, gender) in zip(offsets, gender_groups):
    means = gender.groupby('size')['total_bill'].mean()
    errs = gender.groupby('size')['total_bill'].sem() * 1.96 #95% CI
    ax.errorbar(means.index-offset, means, marker='o', yerr=errs, lw=2)

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159
0

You can set the dodge value in the arguments to a float, for example, dodge=2 would do the trick.

Far
  • 1