【发布时间】:2021-09-20 12:26:45
【问题描述】:
我正在尝试使用带有旋转轴的 seaborn facetgrid 子图,但我似乎无法旋转所有轴,只能旋转最后一个轴。有小费吗?下面的代码使图表只剩下最后一个带有旋转轴的子图。
# Create a grid : initialize it
g = sns.FacetGrid(df_full_year, col='segment', hue='segment', col_wrap=4,sharey=False,sharex=False)
# Add the line over the area with the plot function
g = g.map(plt.bar, 'Currency', 'received_usd')
# Fill the area with fill_between
g = g.map(plt.bar, 'Currency', 'received_usd', alpha=.6).set_titles("{col_name} country")
# Control the title of each facet
g = g.set_titles("{col_name}")
# Add a title for the whole plot
plt.subplots_adjust(top=.92)
g = g.fig.suptitle('Evolution of the value of stuff in 16 countries')
plt.xticks(rotation=45)
# Show the graph
plt.show()
【问题讨论】:
-
请不要不断地将
g分配给其他函数的结果。只需从g = sns.FacetGrid(...)开始,别管它。您可以使用g.set_xticklabels(rotation=45),参见例如How to rotate xticklabels in a seaborn catplot -
谢谢,但事实并非如此。上面的代码仅旋转 facetgrid 中最后一个图表上的轴。我可以使用 catplot,但只是想知道为什么它只旋转最后一张图表上的数据。
-
假设您使用的是最新的 seaborn 和 matplotlib 版本,
g.set_xticklabels(rotation=45)会旋转所有子图的所有可见刻度标签。但是您确实需要从g = g.fig.suptitle(...)中删除g =,因为g.fig.suptitle()的结果不再是FacetGrid。关于catplot的链接帖子并不意味着您需要使用catplot。catplot只是另一个返回FacetGrid的函数,可以以相同的方式设置标签旋转。
标签: python seaborn facet-grid