mvd
mvd

Reputation: 1199

xlabels do not show up with seaborn and tight despined layout in python

I would like to plot the following dataframe with xtick labels rotated while also having a tight, despined layout (using tight_layout() from matplotlib and despine from seaborn). the following does not work because the labels do not show up:

import matplotlib.pylab as plt
import seaborn as sns
import pandas
df = pandas.DataFrame({"x": ["XYZ1", "XYZ2", "XYZ3", "XYZ4"],
                       "y": [0, 1, 0, 1]})
plt.figure(figsize=(5,5))
sns.set_style("ticks")
g = sns.pointplot(x="x", y="y", data=df)
sns.despine(trim=True, offset=2)
g.set_xticklabels(g.get_xticklabels(), rotation=55, ha="center")
plt.tight_layout()

it produces:

enter image description here

the xtick labels ("XYZ1", "XYZ2", ...) do not appear. if i remove despine the ticks appear but then not despined. if i change ticks before despine/tight_layout, they appear but are not rotated. how can this be done?

Upvotes: 1

Views: 4240

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

on my machine the following works

import matplotlib.pylab as plt
import seaborn as sns
import pandas
df = pandas.DataFrame({"x": ["XYZ1", "XYZ2", "XYZ3", "XYZ4"],
                       "y": [0, 1, 0, 1]})
plt.figure(figsize=(5,5))
sns.set_style("ticks")
g = sns.pointplot(x="x", y="y", data=df)
sns.despine(trim=True, offset=2)
g.set_xticklabels(df['x'], rotation=55, ha="center")
plt.tight_layout()
plt.show()

and produces

enter image description here

Upvotes: 3

Related Questions