You need to enable JavaScript to run this app. matplotlib访问ax[i]报TypeError: 'int'对象不可下标问题求助
优惠活动
大模型
产品
解决方案
定价
更多

matplotlib访问ax[i]报TypeError: 'int'对象不可下标问题求助

问题原因
  • 触发TypeError: 'int' object is not subscriptable的核心原因是你在create_plot函数内部错误引用了全局变量well:你在测试代码中已将well赋值为整数0,执行well[depth_curve]这类下标取值操作时,就会触发整数不可被索引的报错。
  • 你的create_plot函数已经通过入参拿到了当前井的数据集dataframe和深度序列depth_curve,无需调用全局的well变量。
修正后的函数代码
import pandas as pd
import matplotlib.pyplot as plt

def create_plot(wellname, dataframe, curves_to_plot, depth_curve, facies_curves=[]):
    num_tracks = len(curves_to_plot)
    
    fig, ax = plt.subplots(nrows=1, ncols=num_tracks, figsize=(num_tracks*2, 10))
    fig.suptitle(wellname, fontsize=20, y=1.05)
    
    for i, curve in enumerate(curves_to_plot):
        ax[i].plot(dataframe[curve], depth_curve)

        ax[i].set_title(curve, fontsize=14, fontweight='bold')
        ax[i].grid(which='major', color='lightgrey', linestyle='-')
        
        ax[i].set_ylim(depth_curve.max(), depth_curve.min())

        if i == 0:
            ax[i].set_ylabel('DEPTH (m)', fontsize=18, fontweight='bold')
        else:
            plt.setp(ax[i].get_yticklabels(), visible = False)
        
        if curve in facies_curves:
            for key in lithology_setup.keys():
                color = lithology_setup[key]['color']
                # 把原来的well全部替换为正确的入参
                ax[i].fill_betweenx(depth_curve, 0, dataframe[curve], where=(dataframe[curve]==key), facecolor=color)
    plt.tight_layout()
    plt.show()

其他测试代码无需改动,运行后即可得到预期的FACIES道填充效果,示例效果如下:
填充效果示例

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 14:24:08