python - 在 seaborn lmplot 中访问轴对象

标签 python matplotlib data-visualization seaborn

<分区>

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

但是,我想创建一个 seaborn.lmplot ,它不返回轴对象。在深入研究了 seaborn.lmplotseaborn.FacetGrid 的文档之后(lmplot 在其后端使用),我发现没有 访问底层axes 对象的方法。此外,虽然大多数其他 seaborn 函数允许您将自己的 axes 作为参数传递,它们将在其上绘制绘图,但 lmplot 不会.

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

有没有办法访问 seaborn.lmplotseaborn.FacetGrid 中的 axes 对象?

最佳答案

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

关于python - 在 seaborn lmplot 中访问轴对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52234914/

相关文章:

python - seaborn/matplotlib 中的散点图,点大小和颜色由连续数据框列给出

python - matplotlib 运行但不生成图形

R:从 ggplot 对象中提取比例名称

python - matplotlib矩阵可视化: controling precision on float values

Python 等价于 Haskell 的 [1..](索引列表)

python - 从 Tensorflow 中的文件队列访问文件名

python - 如何使用 OpenCV 从底部填充图像直到检测到边缘?

Python Django 奇怪地映射到意外的 URL

python-3.x - 如何使用 for 循环并排绘制图表

python - 在二维可视化簇时,轴上的数字意味着什么?