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
相关产品推荐
相关产品推荐

