在seaborn lmplot访问轴对象[复制]

问题描述 投票:2回答:1

这个问题在这里已有答案:

大多数seaborn绘图函数(例如seaborn.barplotseaborn.regplot)在调用时返回matplotlib.pyplot.axes,以便您可以使用此对象根据需要进一步自定义绘图。

但是,我想创建一个seaborn.lmplot,它不返回axis对象。在深入了解seaborn.lmplotseaborn.FacetGridlmplot在其后端使用)的文档后,我发现无法访问底层的axes对象。此外,虽然大多数其他seaborn功能允许您传递自己的axes作为他们将绘制情节的参数,lmplot不。

我想到的一件事是使用plt.gca(),但它只返回网格的最后一个axes对象。

有没有办法访问axesseaborn.lmplot中的seaborn.FacetGrid对象?

python matplotlib data-visualization seaborn
1个回答
2
投票

是的,您可以像这样访问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()

© www.soinside.com 2019 - 2024. All rights reserved.