【问题标题】:Filling the area between plots after setting X axis limits设置 X 轴限制后填充绘图之间的区域
【发布时间】:2020-05-09 02:27:43
【问题描述】:

当我在同一个图表上绘制两条曲线时,我很难设置 X 轴的特定限制。

我的数据有两条曲线(PERMEABILITY 和 POROSITY),DEPTH 将像索引一样工作。所以我设法将它们绘制在同一张图上,并在一些帮助下填充它们之间的区域。这是我的代码:

df = pd.DataFrame({'DEPTH': [100, 150, 200, 250, 300, 350, 400, 450, 500, 550],
       'PERMEABILITY': [1000, 800, 900, 600, 200, 250, 400, 300, 100, 200],
       'POROSITY': [0.30, 0.25, 0.15, 0.19, 0.15, 0.10, 0.15, 0.19, 0.10, 0.15]})

f, ax1 = plt.subplots()

ax1.set_xlabel('PERMEABILITY') 
ax1.set_ylabel('DEPTH')
ax1.set_ylim(df['DEPTH'].max(), df['DEPTH'].min())

ax1.plot(df['PERMEABILITY'], df['DEPTH'], color='red')
ax1.tick_params(axis='x', labelcolor='red')

ax2 = ax1.twiny()

ax2.set_xlabel('POROSITY')
ax2.plot(df['POROSITY'], df['DEPTH'], color='blue')
ax2.tick_params(axis='x', labelcolor='blue')

# convert POROSITY axis to PERMEABILITY
# value-min / range -> normalized POROSITY (normp)
# normp*newrange + newmin -> stretched POROSITY to PERMEABILITY
z=df['POROSITY']
x=df['PERMEABILITY']
nz=((z-np.min(z))/(np.max(z)-np.min(z)))*(np.max(x)-np.min(x))+np.min(x)

# fill between in green where PERMEABILITY is larger
ax1.fill_betweenx(df['DEPTH'],x,nz,where=x>=nz,interpolate=True,color='g')
# fill between in yellow where POROSITY is larger
ax1.fill_betweenx(df['DEPTH'],x,nz,where=x<=nz,interpolate=True,color='y')
plt.show()

但是,当我尝试为 X 轴设置特定限制时,该填充区域不遵循新的“曲线大小”,如下左图所示。我的结果应该像右边的图像(我在 Paint 上做了这个)。例如,如果我添加:

ax1.set_xlim(0, 1500)
ax2.set_xlim(-0.10, 0.45)

有人可以帮我解决这个问题吗?提前致谢!

【问题讨论】:

    标签: python-3.x matplotlib plot


    【解决方案1】:

    您对nz 的计算在两个 x 轴刻度之间转换。当您将两个比例更改不同的数量时,您必须更改nz 的计算。你需要弄清楚如何精确地做到这一点,但在这里我只是观察斜率并偏移直到它匹配。

    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np
    
    df = pd.DataFrame({'DEPTH': [100, 150, 200, 250, 300, 350, 400, 450, 500, 550],
           'PERMEABILITY': [1000, 800, 900, 600, 200, 250, 400, 300, 100, 200],
           'POROSITY': [0.30, 0.25, 0.15, 0.19, 0.15, 0.10, 0.15, 0.19, 0.10, 0.15]})
    
    f, ax1 = plt.subplots()
    
    ax1.set_xlabel('PERMEABILITY') 
    ax1.set_ylabel('DEPTH')
    ax1.set_ylim(df['DEPTH'].max(), df['DEPTH'].min())
    
    ax1.plot(df['PERMEABILITY'], df['DEPTH'], color='red')
    ax1.tick_params(axis='x', labelcolor='red')
    
    ax2 = ax1.twiny()
    
    ax2.set_xlabel('POROSITY')
    ax2.plot(df['POROSITY'], df['DEPTH'], color='blue')
    ax2.tick_params(axis='x', labelcolor='blue')
    
    # convert POROSITY axis to PERMEABILITY
    # value-min / range -> normalized POROSITY (normp)
    # normp*newrange + newmin -> stretched POROSITY to PERMEABILITY
    z=df['POROSITY']
    x=df['PERMEABILITY']
    nz=((z-np.min(z))/(np.max(z)-np.min(z)))*(np.max(x)-np.min(x))*0.6+np.min(x)+450
    #                                                          slope ^      offset ^
    
    ax1.set_xlim(0, 1500)
    ax2.set_xlim(-0.10, 0.45)
    # fill between in green where PERMEABILITY is larger
    ax1.fill_betweenx(df['DEPTH'],x,nz,where=x>=nz,interpolate=True,color='g')
    # fill between in yellow where POROSITY is larger
    ax1.fill_betweenx(df['DEPTH'],x,nz,where=x<=nz,interpolate=True,color='y')
    plt.show()
    

    此外,如果您在代码 sn-p 中包含所有必要的导入,则更容易回答您的问题。

    【讨论】:

    • 谢谢,唐柯比!你已经回答了我的问题,但如果你允许我,我还有一个。我知道偏移量是 df['DEPTH'].max() - df['DEPTH'].min() 并且斜率应该是这个偏移量除以 (df['PERMEABILITY'][0] - df['渗透性'][9])。那正确吗?。但是如果我在孔隙度上反转我的 x 轴,它应该像 ax2.set_xlim(0.45, -0.10) 然后我不知道如何改变这个斜率方程。能给我看看吗?
    • 即使我将 np.min(z) 更改为 np.max(z) ,反之亦然,我也无法得到准确的情节。或者也许我理解错了你的 0.6 值,你可以告诉我你是如何看待这个的