Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - Filling the area between two lines on different scales/subplot axes

I am trying to figure out how to fill between two lines that are on different scales & axes of subplot, however, I have not been able to figure out how to do this.

I have tried following the answer here for a similar question, but the formula supplied in the code doesn't work on my dataset and based on the responses from the author of that question the equation doesn't appear to work when the x limits are changed.

The following image is what I am after (created in Photoshop):

Shaded Plot

However, using the code below, I get:

Incorrectly Shaded Plot

Example Data & Code

import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({'DEPTH':[4300, 4310, 4320, 4330, 4340, 4350, 4360, 4370, 4380, 4390], 
                     'NEUT':[45, 40, 30, 12, 6, 12, 8, 10, 20, 18], 
                     'DENS':[2.5, 2.55, 2.32, 2.35, 2.3, 2.55, 2.58, 2.6, 2.52, 2.53]})

fig = plt.subplots(figsize=(7,20))

ax1 = plt.subplot2grid((1,1), (0,0))
ax2 = ax1.twiny()

ax1.plot('DENS', 'DEPTH', data=data, color='red')
ax1.set_xlim(1.95, 2.95)
ax1.set_xlabel('Density')
ax1.xaxis.label.set_color("red")
ax1.tick_params(axis='x', colors="red")
ax1.spines["top"].set_edgecolor("red")

ax2.plot('NEUT', 'DEPTH', data=data, color='blue')
ax2.set_xlim(45, -15)
ax2.set_xlabel('Neutron')
ax2.xaxis.label.set_color("blue")
ax2.spines["top"].set_position(("axes", 1.04))
ax2.tick_params(axis='x', colors="blue")
ax2.spines["top"].set_edgecolor("blue")

ax1.fill_betweenx(data['DEPTH'], data['DENS'], data['NEUT'], where=data['DENS']>=data['NEUT'], interpolate=True, color='green')
ax1.fill_betweenx(data['DEPTH'], data['DENS'], data['NEUT'], where=data['DENS']<=data['NEUT'], interpolate=True, color='yellow')

for ax in [ax1, ax2]:
    ax.set_ylim(4400, 4300)
    ax.xaxis.set_ticks_position("top")
    ax.xaxis.set_label_position("top")

Would anyone be able to help me with this please?

question from:https://stackoverflow.com/questions/65905036/filling-the-area-between-two-lines-on-different-scales-subplot-axes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The difference between your code and the answer you linked is that your Neutron scale goes from the maximum value on the left to the minimum value on the right, which means the logic is slightly wrong. So we just need to switch a few min and max terms around.

Try this:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame({'DEPTH':[4300, 4310, 4320, 4330, 4340, 4350, 4360, 4370, 4380, 4390], 
                     'NEUT':[45, 40, 30, 12, 6, 12, 8, 10, 20, 18], 
                     'DENS':[2.5, 2.55, 2.32, 2.35, 2.3, 2.55, 2.58, 2.6, 2.52, 2.53]})

fig = plt.subplots(figsize=(6,8))

ax1 = plt.subplot2grid((1,1), (0,0))
ax2 = ax1.twiny()

ax1.plot('DENS', 'DEPTH', data=data, color='red')
ax1.set_xlim(1.95, 2.95)
ax1.set_xlabel('Density')
ax1.xaxis.label.set_color("red")
ax1.tick_params(axis='x', colors="red")
ax1.spines["top"].set_edgecolor("red")

ax2.plot('NEUT', 'DEPTH', data=data, color='blue')
ax2.set_xlim(45, -15)
ax2.set_xlabel('Neutron')
ax2.xaxis.label.set_color("blue")
ax2.spines["top"].set_position(("axes", 1.08))
ax2.tick_params(axis='x', colors="blue")
ax2.spines["top"].set_edgecolor("blue")

x = np.array(ax1.get_xlim())
z = np.array(ax2.get_xlim())

x1 = data['DENS']
x2 = data['NEUT']

nz=((x2-np.max(z))/(np.min(z)-np.max(z)))*(np.max(x)-np.min(x))+np.min(x)

ax1.fill_betweenx(data['DEPTH'], x1, nz, where=x1>=nz, interpolate=True, color='green')
ax1.fill_betweenx(data['DEPTH'], x1, nz, where=x1<=nz, interpolate=True, color='yellow')

for ax in [ax1, ax2]:
    ax.set_ylim(4400, 4300)
    ax.xaxis.set_ticks_position("top")
    ax.xaxis.set_label_position("top")

plt.show()

(I changed the figure size so it would fit on my screen)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...