python - matplotlib 访问 ax[i] 导致 TypeError: 'int' object is not subscriptable
问题描述
我正在尝试在 matplotlib 中制作自定义图,其中子图在 0 和基于 pandas 数据列中的值的值之间着色。我已将此代码从使用固定轴值(例如ax1.fillbetweenx()
)转换为当前代码以访问轴数组的部分。
但是,当我运行该函数时,我TypeError: 'int' object is not subscriptable
从以下行收到一个:
ax[i].fill_betweenx(well[depth_curve], 0, well['FACIES'], where=(well[curve]==key), facecolor=color)
任何帮助,将不胜感激。谢谢!
功能:
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']
ax[i].fill_betweenx(well[depth_curve], 0, 4, where=(well[curve]==key), facecolor=color)
plt.tight_layout()
plt.show()
代码和示例数据:
workingdf = pd.DataFrame({'WELL':['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
'DEPTH':[4300, 4310, 4320, 4330, 4340, 4350, 4360, 4370, 4380, 4390],
'GR':[45, 40, 30, 12, 6, 12, 8, 10, 20, 18],
'FACIES':[1, 1, 1, 1, 2, 1, 2, 2, 3, 3]})
lithology_setup = {1: {'lith':'Sandstone', 'color':'#ffff00'},
2: {'lith':'Sandstone/Shale', 'color':'#ffe119'},
3: {'lith':'Shale', 'color':'#bebebe'},}
curves_to_plot = ['GR', 'FACIES']
facies_curve=['FACIES']
grouped = workingdf.groupby('WELL')
# Create empty lists
dfs_wells = []
wellnames = []
#Split up the data by well
for well, data in grouped:
dfs_wells.append(data)
wellnames.append(well)
well = 0
create_plot(wellnames[well],
dfs_wells[well],
curves_to_plot,
dfs_wells[well]['DEPTH'],
facies_curve)
下面是我之前代码的最后一个子图中的阴影效果示例。此处的代码片段仅用于为子图着色而没有任何阴影。
解决方案
感谢 tmdavison 的评论。该问题与使用well
而不是在行dataframe
中有关ax[i].fill_betweenx()
代码行从:
ax[i].fill_betweenx(well[depth_curve], 0, 4, where=(well[curve]==key),
facecolor=color)
至
ax[i].fill_betweenx(depth_curve, 0, 4, where=(dataframe[curve]==key),
facecolor=color)
固定功能如下
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']
ax[i].fill_betweenx(depth_curve, 0, dataframe[curve],
where=(dataframe[curve]==key),
facecolor=color)
plt.tight_layout()
plt.show()
这会生成以下图:
此外,更新了原始示例数据以允许出现填充。
推荐阅读
- c - 线程数组中的第一个线程在c中被跳过(有时)?
- javascript - 如何观察通过 setter-getter 或 Proxy 暴露的对象数组属性内容的变化
- mysql - 如何将查询结果连接到另一个查询
- python - 制作轮盘游戏,python中不断报错
- php - 使用 Orchestral/testbench 时,Mix 清单不存在
- javascript - JS和Python之间的基准性能差异
- android - 从 Firebase 实时数据库查询嵌套数据以填充 ListView
- google-sheets - 如何在谷歌工作表中的不同工作表上使用多个脚本?
- javascript - 带有 Puppeteer 的 localStorage 的奇怪行为者
- racket - 如何使用旧版本的包 gregor?