如何在图中以不同比例填充曲线之间的区域? [英] How to fill areas between curves with different scales in a plot?

查看:62
本文介绍了如何在图中以不同比例填充曲线之间的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下三个功能的数据框:深度,渗透性和孔隙度.我想在y轴上绘制DEPTH,在x轴上绘制渗透率和孔隙率,尽管最后两个特征的比例不同.

  df = pd.DataFrame({'DEPTH(m)':[100,150,200,250,300,350,400,450,500,550],"PERMEABILITY(mD)":[1000、800、900、600、200、250、400、300、100、200],'孔隙度(%)':[0.30、0.25、0.15、0.19、0.15、0.10、0.15、0.19、0.10、0.15]}) 

我已经设法将它们绘制在一起,但是现在我需要用两种不同的颜色填充曲线之间的区域.例如,当渗透性"曲线位于孔隙度"的右侧时,它们之间的区域应为绿色.如果PERPERBILITY位于左侧,则曲线之间的区域应为黄色.

  f,ax1 = plt.subplots()ax1.set_xlabel('PERMEABILITY(mD)')ax1.set_ylabel('DEPTH(m)')ax1.set_ylim(df ['DEPTH(m)'].max(),df ['DEPTH(m)'].min())ax1.plot(df ['PERMEABILITY(mD)'],df ['DEPTH(m)'],color ='red')ax1.tick_params(axis ='x',labelcolor ='red')ax2 = ax1.twiny()ax2.set_xlabel('POROSITY(%)')ax2.plot(df ['POROSITY(%)'],df ['DEPTH(m)'],color ='blue')ax2.tick_params(axis ='x',labelcolor ='blue') 

所以正确的输出应该是这样的:(对不起下面的绘画"图像)

有人可以帮我吗?

解决方案

您可以使用

I have a dataframe with three features: DEPTH, PERMEABILITY and POROSITY. And I would like to plot DEPTH at y axis and PERMEABILITY and POROSITY together at x axis, although these last two features have different scales.

df = pd.DataFrame({'DEPTH(m)': [100, 150, 200, 250, 300, 350, 400, 450, 500, 550],
           'PERMEABILITY(mD)': [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]})

I already managed to plot them together, but now I need to fill with two different colors the areas between the curves. For example, when PERMEABILITY curve is on the right side of POROSITY, the area between them should be green. If PERMEABILITY is on the left side, the area between curves should be yellow.

f, ax1 = plt.subplots()

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

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

ax2 = ax1.twiny()

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

So the right output should be like this: (Sorry for the Paint image below)

Anyone could help me with this?

解决方案

You can use the fill_betweenx() function, however you need to convert one of your axis to the scale of the other one, because you use twiny. Below, I converted your POROSITY data to fit to the axis of PERMEABILITY.

Then you can use two conditional fill_betweenx, where the two curves are larger than each other, to assign different colors to those patches. Also, since your data is discrete, you need to set interpolate=True in your fill_betweenx functions.

f, ax1 = plt.subplots()

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

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

ax2 = ax1.twiny()

ax2.set_xlabel('POROSITY(%)')
ax2.plot(df['POROSITY(%)'], df['DEPTH(m)'], 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(mD)']
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(m)'],x,nz,where=x>=nz,interpolate=True,color='g')
# fill between in yellow where POROSITY is larger
ax1.fill_betweenx(df['DEPTH(m)'],x,nz,where=x<=nz,interpolate=True,color='y')
plt.show()

The result is as below (I might have used different colors, but I assume that's not a concern).

这篇关于如何在图中以不同比例填充曲线之间的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆