You need to enable JavaScript to run this app. SentinelHub-py导入plot_image报错及无法正常绘图问题求助
优惠活动
大模型
产品
解决方案
定价
更多

SentinelHub-py导入plot_image报错及无法正常绘图问题求助

问题原因及解决步骤

你当前遇到的问题是三个逻辑错误共同导致的:

  1. 导入语句执行顺序错误,且属于冗余导入
  2. 自定义plot_image函数存在语法缺陷
  3. 缺少拉取图像、调用绘图函数的核心逻辑

步骤1:删除冗余导入语句

直接删掉代码里的from utils import plot_image行,你已经把绘图函数写在当前脚本中,不需要再从外部utils包导入。

步骤2:调整函数定义位置

把plot_image函数的定义移到所有导入语句之后、你调用该函数的代码之前,避免出现“函数未定义”的报错。

步骤3:修复plot_image函数的语法问题

你写的函数中直接使用了**kwargs但没有将其声明为入参,运行时会触发NameError,修改后的函数代码如下:

"""
Utilities used by example notebooks
"""
import matplotlib.pyplot as plt
import numpy as np

def plot_image(image, factor=3.5/255, clip_range=(0,1), **kwargs):
    """
    Utility function for plotting RGB images.
    """
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
    if clip_range is not None:
        ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
    else:
        ax.imshow(image * factor, **kwargs)
    ax.set_xticks([])
    ax.set_yticks([])

步骤4:补全拉取图像、调用绘图的代码

你当前贴出的代码仅完成了参数初始化,还没有构造请求拉取卫星数据,也没有调用绘图函数,自然不会出图。你可以参考以下代码补全逻辑:

# 真彩色合成的evalscript
evalscript_true_color = """
    //VERSION=3
    function setup() {
        return {
            input: [{
                bands: ["B2", "B3", "B4"]
            }],
            output: {
                bands: 3,
                sampleType: "AUTO"
            }
        };
    }
    function evaluatePixel(sample) {
        return [sample.B4, sample.B3, sample.B2];
    }
"""

# 构造拉取Sentinel-2 L2A数据的请求
request_true_color = SentinelHubRequest(
    data_collection=DataCollection.SENTINEL2_L2A,
    bbox=betsiboka_bbox,
    size=betsiboka_size,
    evalscript=evalscript_true_color,
    config=config,
    time_interval=("2023-06-01", "2023-06-30"),
    mosaicking_order="leastCC"
)

# 拉取数据并绘图
true_color_image = request_true_color.get_data()[0]
plot_image(true_color_image)
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 17:06:03