How can I adjust the spacing between labels in a factorplot in Seaborn

2k views Asked by At

I've been thoroughly enjoying the amazing combination of Pandas and Seaborn for my data analysis and plotting needs. It's been enough to prevent me from going down the path of learning R just for dataframes and ggplot ;P. I'm having a small issue with the factorplot in seaborn and the way that it places labels on the x-axis. Below is the example that's causing me trouble:

"squished" x-axis labels

Basically, I want to "fix" the x-axis labels so that the final column ">=35" isn't so "squished" (i.e. overlapping the preceding label). Is there an easy way to do this? I came up with a temporary solution of encoding ">=" as unicode and adding an extra space before the label, but it would be nicer to have a general solution to enforce spacing between labels.

1

There are 1 answers

2
mwaskom On BEST ANSWER

These are just matplotlib axis ticklabels at integral positions. So you could do

df = pd.DataFrame(dict(x=np.repeat(np.arange(21), 10), y=np.random.randn(210)))
df.loc[df.x == 20, "x"] = ">= 20"

g = sns.factorplot(x="x", y="y", data=df, kind="box")
g.axes[0, 0].set_xticks(range(20) + [20.5])

enter image description here

(Note that version version 0.6+ has an ax attribute on single-axes FacetGrid objects that will make accessing the methods a bit easier)