xlabels没有出现在python中的seaborn和紧密的despined布局

时间:2016-09-29 16:40:14

标签: python matplotlib seaborn

我想绘制以下数据框,其中xtick标签已旋转,同时还有一个紧凑的,受到限制的布局(使用来自matplotlib的tight_layout()和来自seaborn的despine)。以下内容无效,因为标签未显示:

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()

它产生:

enter image description here

xtick标签(" XYZ1"," XYZ2",...)不会出现。如果我去除despine蜱出现,但然后没有被鄙视。如果我在despine / tight_layout之前更改刻度,它们会出现但不会旋转。如何才能做到这一点?

1 个答案:

答案 0 :(得分:2)

在我的机器上

以下工作

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()

并制作

enter image description here