You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Seaborn v0.12新对象API:如何获取底层Matplotlib Figure/Axes对象?

Seaborn v0.12新对象API获取Matplotlib对象方案

在Seaborn的新对象API中,so.Plot实例本身并非Matplotlib的Axes/Figure对象,需通过以下方式获取底层绘图对象:

1. 调用.plot()获取Axes实例

调用Plot对象的.plot()方法会直接返回对应的Matplotlib Axes对象,之后就能调用Axes的各种方法:

import seaborn as sns
from seaborn import objects as so

tips = sns.load_dataset('tips')
plot = (
    so.Plot(tips, "size", "tip")
    .add(so.Line(), so.Agg())
)
# 获取Axes对象并设置标题
ax = plot.plot()
ax.set_title("Custom Title")

2. 从Axes获取Figure对象

若需要操作Figure,可通过已获取的Axes对象的.figure属性获取:

fig = ax.figure
# 例如调整Figure尺寸
fig.set_size_inches(10, 6)

3. 在现有Axes上绘制

如果想复用已创建的Matplotlib Axes,可将其传入.plot()的ax参数:

import matplotlib.pyplot as plt

# 创建自定义Axes
fig, custom_ax = plt.subplots(figsize=(8, 4))
# 在指定Axes上绘制
plot.plot(ax=custom_ax)
custom_ax.set_xlabel("Party Size")
custom_ax.set_ylabel("Average Tip")

内容的提问来源于stack exchange,提问作者Probability is wonderful

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:55:18