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

如何创建适配大边界的自定义45度对数图网格线

解决旋转45度对数网格覆盖不全的问题

问题描述

尝试制作带顺时针旋转45度网格的对数图,当边界范围较小时(xmin = 1,xmax = 10,ymin = 1,ymax = 3)代码正常运行;但扩大边界范围后(xmin = 1,xmax = 100,ymin = 1,ymax = 30),网格无法覆盖整个绘图区域。

解决方案

核心是正确使用ExtremeFinderFixed指定网格线需要覆盖的转换后坐标系极值范围,同时修正原代码中坐标范围的计算错误。

关键步骤说明

  • 计算转换后坐标系极值:取原坐标系四个角点((xmin, ymin)、(xmax, ymin)、(xmin, ymax)、(xmax, ymax)),通过自定义转换函数计算其在转换后坐标系的坐标,提取x、y方向的最小/最大值,作为ExtremeFinderFixed的参数。
  • 修正轴范围:直接基于转换后的极值设置轴范围,替代原代码中错误的计算逻辑。
  • 调整网格定位器:使用对数间距生成网格点,更贴合对数图的特性。

修正后的完整代码

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist import SubplotHost, GridHelperCurveLinear
from mpl_toolkits.axisartist.grid_finder import FixedLocator
from mpl_toolkits.axisartist.floating_axes import ExtremeFinderFixed

fig = plt.figure()

# 原坐标(x,y) → 转换后坐标(u,v)
def reciprocity_tr(x, y): 
    return np.log10(x) + np.log10(y), np.log10(y) - np.log10(x)
# 转换后坐标(u,v) → 原坐标(x,y)
def reciprocity_inv_tr(u, v): 
    return 10 ** ((u - v) / 2), 10 ** ((u + v) / 2)

# 原坐标系边界
xmin = 1
xmax = 100
ymin = 1
ymax = 30

# 计算四个角点的转换后坐标
corners = [
    reciprocity_tr(xmin, ymin),
    reciprocity_tr(xmax, ymin),
    reciprocity_tr(xmin, ymax),
    reciprocity_tr(xmax, ymax)
]
# 提取转换后坐标系的极值
u_vals = [p[0] for p in corners]
v_vals = [p[1] for p in corners]
u_min, u_max = min(u_vals), max(u_vals)
v_min, v_max = min(v_vals), max(v_vals)

# 生成对数间距的网格定位点
grid_locator1 = FixedLocator(np.logspace(np.log10(xmin), np.log10(xmax), num=10))
grid_locator2 = FixedLocator(np.logspace(np.log10(ymin), np.log10(ymax), num=10))

# 配置网格助手,传入极值范围确保网格覆盖整个区域
extreme_finder = ExtremeFinderFixed((u_min, v_min, u_max, v_max))
reciprocity_grid_helper = GridHelperCurveLinear(
    (reciprocity_tr, reciprocity_inv_tr),
    extreme_finder=extreme_finder,
    grid_locator1=grid_locator1,
    grid_locator2=grid_locator2
)

# 创建子图并设置轴范围
reciprocityAxis = SubplotHost(fig, 111, grid_helper=reciprocity_grid_helper)
reciprocityAxis.set_xlim(u_min, u_max)
reciprocityAxis.set_ylim(v_min, v_max)

fig.add_axes(reciprocityAxis)
reciprocityAxis.grid(True)
reciprocityAxis.set_aspect('equal')

plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 19:02:21