【发布时间】:2018-06-11 02:52:49
【问题描述】:
我使用以下代码在 python 中使用 seaborn 生成计数图:
sns.countplot( x='Genres', data=gn_s)
但我得到以下输出:
我无法清楚地看到 x 轴上的项目,因为它们是重叠的。我该如何纠正? 此外,我希望所有项目按数量递减顺序排列。我怎样才能做到这一点?
【问题讨论】:
我使用以下代码在 python 中使用 seaborn 生成计数图:
sns.countplot( x='Genres', data=gn_s)
但我得到以下输出:
我无法清楚地看到 x 轴上的项目,因为它们是重叠的。我该如何纠正? 此外,我希望所有项目按数量递减顺序排列。我怎样才能做到这一点?
【问题讨论】:
您可以使用选择x-axis 为垂直,例如:
g = sns.countplot( x='Genres', data=gn_s)
g.set_xticklabels(g.get_xticklabels(),rotation=90)
或者,您也可以这样做:
plt.xticks(rotation=90)
【讨论】:
sns.countplot中的order参数,例如sns.countplot( x='Genres', data=gn_s, order = gn_s['Genres'].value_counts().index)
引入 matplotlib 以提前设置轴,以便您可以通过将轴刻度标签旋转 90 度和/或更改字体大小来修改它们。要按顺序排列示例,您需要修改源。我假设您是从 pandas 数据框开始的,例如:
data = data.sort_values(by='Genres', ascending=False)
labels = # list of labels in the correct order, probably your data.index
fig, ax1 = plt.subplots(1,1)
sns.countplot( x='Genres', data=gn_s, ax=ax1)
ax1.set_xticklabels(labels, rotation=90)
可能会有帮助。
edit 接受来自 cmets 的 andrewnagyeb 的建议来订购情节:
sns.countplot( x='Genres', data=gn_s, order = gn_s['Genres'].value_counts().index)
【讨论】: