在seaborn的lmplot中访问坐标轴对象

8
大多数 seaborn 绘图函数(例如 seaborn.barplot, seaborn.regplot)在调用时会返回一个 matplotlib.pyplot.axes 对象,以便您可以使用该对象进一步自定义绘图。
然而,我想创建一个 seaborn.lmplot,但它不返回 axes 对象。在查阅了 seaborn.lmplotseaborn.FacetGridlmplot 在其后端中使用)的文档后,我发现没有办法访问底层的 axes 对象。此外,虽然大多数其他 seaborn 函数允许您将自己的 axes 作为参数传递,以便它们在其上绘制图形,但 lmplot 却不允许这样做。
我想到的一件事是使用plt.gca(),但那只返回网格中最后一个axes对象。
有没有办法访问seaborn.lmplotseaborn.FacetGrid中的axes对象?
1个回答

9

是的,您可以像这样访问matplotlib.pyplot.axes对象:

import seaborn as sns
lm = sns.lmplot(...)  # draw a grid of plots
ax = lm.axes  # access a grid of 'axes' objects

在这里,ax是一个包含所有子图坐标轴对象的数组。你可以像这样访问每个对象:

ax.shape  # see the shape of the array containing the 'axes' objects
ax[0, 0]  # the top-left (first) subplot 
ax[i, j]  # the subplot on the i-th row of the j-th column

如果只有一个子图,您可以通过我上面展示的方式访问它(使用ax[0, 0]),或者根据您在问题中提出的方式通过plt.gca()访问。


1
哇,这么简单…我现在觉得有些傻!非常感谢! - nazz
不用谢!:) - Djib2011

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接