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

Python中如何从matplotlib绘制的线对象生成坐标点列表

解决方案

你绘制的是两点确定的线性趋势线,有两种常用方式可以快速查询任意x对应的y值:

方式1:直接通过线性公式计算(性能最高,适合线性趋势线场景)

两点确定的直线可以直接用斜率截距公式计算任意点数值,不需要依赖matplotlib返回对象:

x1 = 1
x2 = 100
y1 = 53000
y2 = 89745

# 计算斜率和截距
slope = (y2 - y1) / (x2 - x1)
intercept = y1 - slope * x1

# 查询x=67对应的数值
target_x = 67
target_y = slope * target_x + intercept
print(target_y)

方式2:从matplotlib返回的Line2D对象插值(通用场景,适合非直线的趋势线)

axes.plot()返回的是Line2D对象的列表,你可以先取出趋势线实例,再通过内置方法拿到坐标点做插值:

import numpy as np

# 取出Line2D实例,plot返回的是长度为1的列表
trend_line = TREND_LINE[0]
# 获取当前趋势线已有的所有坐标点
x_points = trend_line.get_xdata()
y_points = trend_line.get_ydata()

# 线性插值得到目标x对应的y值
target_x = 67
target_y = np.interp(target_x, x_points, y_points)
print(target_y)

如果你的趋势线是更复杂的拟合曲线(比如多项式、样条曲线),只需要传入全量的坐标点到np.interp即可完成查询,不需要自己手动计算拟合参数。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 17:57:05