【问题标题】:Access axes object in seaborn lmplot [duplicate]在seaborn lmplot中访问轴对象[重复]
【发布时间】:2019-02-13 12:54:29
【问题描述】:

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

但是,我想创建一个seaborn.lmplot,它不会返回坐标区对象。在深入研究了 seaborn.lmplotseaborn.FacetGridlmplot 在其后端使用)的文档后,我发现 no 访问底层 axes 对象的方法。此外,虽然大多数其他 seaborn 函数允许您将自己的 axes 作为参数传递,但lmplot 不能

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

有什么方法可以访问seaborn.lmplotseaborn.FacetGrid 中的axes 对象吗?

【问题讨论】:

    标签: python matplotlib data-visualization seaborn


    【解决方案1】:

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

    【讨论】:

    • 哇,就这么简单……我现在觉得自己很蠢!非常感谢!
    • 不客气! :)
    猜你喜欢
    • 2014-07-17
    • 2021-09-06
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2019-02-02
    • 2013-01-31
    • 2016-07-08
    相关资源
    最近更新 更多