【问题标题】:xlabels do not show up with seaborn and tight despined layout in pythonxlabels 在 python 中不显示 seaborn 和紧凑的布局
【发布时间】:2016-09-29 16:40:13
【问题描述】:

我想用旋转的 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()

它产生:

不会出现 xtick 标签(“XYZ1”、“XYZ2”、...)。如果我删除绝望的蜱虫出现,但随后不绝望。如果我在 despine/tight_layout 之前更改刻度,它们会出现但不会旋转。如何才能做到这一点?

【问题讨论】:

  • 你可以做plt.setp(ax.get_xticklabels(), rotation=50)

标签: python matplotlib seaborn


【解决方案1】:

在我的机器上,以下工作

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

并产生

【讨论】:

  • 我想知道为什么你在 y 轴上得到 y 而我的版本产生 mean(y)
猜你喜欢
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多