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

如何在Optuna帕累托前沿图中设置坐标轴等比例?

解决Optuna帕累托前沿图坐标轴等比例问题

针对你遇到的两种场景,分别给出可行方案:

一、交互式Plotly图实现等比例坐标轴

Optuna默认的plot_pareto_front返回Plotly交互式图,可通过修改布局参数强制坐标轴比例一致,无需手动调整:

import optuna
from optuna.visualization import plot_pareto_front

# 假设已完成多目标优化,得到study对象
fig = plot_pareto_front(study)

# 强制x、y轴比例相等,保持正方形比例
fig.update_layout(
    xaxis={"scaleanchor": "y", "scaleratio": 1},
    yaxis={"scaleanchor": "x", "scaleratio": 1}
)

fig.show()

scaleanchor将x轴与y轴绑定,scaleratio=1确保两者比例严格一致,无论窗口大小如何变化,坐标轴比例都保持正方形。

二、Matplotlib图关联自定义轴并设置等比例

使用Matplotlib接口时,必须将自定义创建的Axes对象传入plot_pareto_front,否则函数会自动新建独立轴,导致你的设置无效:

import matplotlib.pyplot as plt
import optuna
from optuna.visualization.matplotlib import plot_pareto_front

# 创建自定义画布和坐标轴
fig, ax = plt.subplots()

# 传入ax参数,让绘图函数使用自定义轴
plot_pareto_front(study, ax=ax)

# 设置坐标轴等比例
ax.set_aspect("equal", adjustable="box")
# 也可使用 ax.axis('equal'),效果一致

plt.show()

通过ax=ax指定绘图使用自定义轴后,后续调用set_aspect或axis('equal')就能正确生效,实现正方形坐标轴。

内容的提问来源于stack exchange,提问作者Carlo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 05:33:57