这个问题在这里已有答案:
大多数seaborn绘图函数(例如seaborn.barplot
,seaborn.regplot
)在调用时返回matplotlib.pyplot.axes
,以便您可以使用此对象根据需要进一步自定义绘图。
但是,我想创建一个seaborn.lmplot
,它不返回axis对象。在深入了解seaborn.lmplot
和seaborn.FacetGrid
(lmplot
在其后端使用)的文档后,我发现无法访问底层的axes
对象。此外,虽然大多数其他seaborn功能允许您传递自己的axes
作为他们将绘制情节的参数,lmplot
不。
我想到的一件事是使用plt.gca()
,但它只返回网格的最后一个axes
对象。
有没有办法访问axes
或seaborn.lmplot
中的seaborn.FacetGrid
对象?
是的,您可以像这样访问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()
)