Matplotlib/Pandas绘图与数据不符问题排查及代码优化咨询
问题诊断与修复方案
核心问题原因
- 数据未按深度排序:深度数据非连续且未按顺序排列,绘图时直接按原始数据顺序连接点,导致线条乱穿,无法反映随深度的变化趋势。
- 子图内容错误:标注为Gamma的第二个子图,实际绘制的是Caliper数据,完全不符合预期。
- 重复创建双X轴导致覆盖:第五个子图中两次创建
ax15,后一次覆盖前一次,导致Static Fluid Temperature曲线丢失。 - 冗余变量转换:将DataFrame列转为列表完全没必要,既浪费内存又不利于后续排序操作。
修复后的代码
import matplotlib.pyplot as plt import pandas as pd # 读取数据并按深度排序(关键修复) df1 = pd.read_excel(r'017_FLD_and_FLS.xlsx').sort_values(by='Depth').reset_index(drop=True) df2 = pd.read_excel(r'017_SUMD_SUMS.xlsx').sort_values(by='Depth').reset_index(drop=True) # 绘图设置 fig = plt.figure(figsize=(15,20)) fig.suptitle('GB017', fontsize=30, x=0.5, y=1.01) # 1. 井径子图(Pre/Post) ax01 = plt.subplot(151) ax01.plot(df2['Caliper (pre-pumping)'], df2['Depth'], color="black") ax01.invert_yaxis() ax01.set_xlabel('Caliper (Inches)', color="black") ax01.set_ylabel('Depth (Feet)', color="black") ax01.grid(True) ax11 = ax01.twiny() ax11.plot(df2['Caliper (post-pumping)'], df2['Depth'], color='green') ax11.set_xlabel('Caliper (post-pumping)', color="green") ax11.tick_params(axis='x', labelcolor="green") ax11.grid(True, linestyle='--') # 2. 自然伽马子图(修复内容错误) ax02 = plt.subplot(152, sharey=ax01) ax02.plot(df2['Natural Gamma'], df2['Depth'], color="black") ax02.set_xlabel('Gamma (counts)', color="black") ax02.grid(True) # 3. 中子-密度子图 ax03 = plt.subplot(153, sharey=ax01) ax03.plot(df2['Spherical Density (long-S)'], df2['Depth'], color='red') ax03.set_xlabel('Density (counts)', color='red') ax03.tick_params(axis='x', labelcolor="red") ax03.grid(True) ax13 = ax03.twiny() ax13.plot(df2['Neutron'], df2['Depth'], color='blue') ax13.invert_xaxis() ax13.set_xlabel('Neutron (counts)', color="blue") ax13.tick_params(axis='x', labelcolor="blue") ax13.grid(True, linestyle='--') # 4. 电导率-电阻率子图 ax04 = plt.subplot(154, sharey=ax01) ax04.plot(df2['Formation Conductivity'], df2['Depth'], color="black") ax04.set_xlabel('Formation Conductivity(mS/cm)', color="black") ax04.tick_params(axis='x', labelcolor="black") ax04.grid(True) ax14 = ax04.twiny() ax14.plot(df2['Formation Resistivity'], df2['Depth'], color='blue') ax14.invert_xaxis() ax14.set_xlabel('Formation Resistivity (ohm-m)', color="blue") ax14.tick_params(axis='x', labelcolor="blue") ax14.grid(True, linestyle='--') # 5. 温度与热脉冲子图(修复双X轴覆盖问题) ax05 = plt.subplot(155, sharey=ax01) ax05.plot(df1['Heat Pulse Flow'], df1['Depth'], color="black") ax05.set_xlabel('Heat Pulse Flow (gpm)', color="black") ax05.set_ylabel('Depth (Feet)', color="black") ax05.grid(True) # 第一个双X轴:静态流体温度 ax15 = ax05.twiny() ax15.plot(df1['Static Fluid Temperature'], df1['Depth'], color='blue') ax15.invert_xaxis() ax15.set_xlabel('Static Fluid Temperature', color="blue") ax15.tick_params(axis='x', labelcolor="blue") # 第二个双X轴:泵送温度Run3(用新变量名避免覆盖) ax25 = ax05.twiny() # 调整轴位置避免重叠 ax25.spines['top'].set_position(('outward', 30)) ax25.plot(df1['Pumping Temperature Run 3'], df1['Depth'], color='orange') ax25.invert_xaxis() ax25.set_xlabel('Pumping Temperature Run 3', color="orange") ax25.tick_params(axis='x', labelcolor="orange") ax25.grid(True, linestyle='--') fig.tight_layout() plt.show()
代码优化建议(保持修改便捷性)
- 统一深度轴同步:所有子图使用
sharey=ax01,确保深度轴联动,修改一次即可全局生效。 - 提取绘图配置字典:把每个子图的曲线信息(数据列、颜色、标签)做成字典列表,循环生成子图,减少重复代码。例如:
plot_configs = [ {"subplot": 151, "curves": [ {"data": ("Caliper (pre-pumping)", "Depth"), "color": "black", "xlabel": "Caliper (Inches)"}, {"data": ("Caliper (post-pumping)", "Depth"), "color": "green", "xlabel": "Caliper (post-pumping)", "twiny": True} ]}, # 其他子图配置... ] - 处理深度对齐:如果df1和df2的深度不完全匹配,可使用
pd.merge按深度合并数据,或用插值补全缺失深度点,确保所有曲线的深度轴完全一致。 - 避免全局plt调用:全程使用面向对象的
ax.plot方式,不要混用plt.plot,代码逻辑更清晰,便于维护。
内容的提问来源于stack exchange,提问作者Corporateshill
相关产品推荐
相关产品推荐

