简体   繁体   English

Matplotlib fill_between() 多行

[英]Matplotlib fill_between() Multiple lines

I am plotting four lines which form an area in between them, an area I am trying to fill.我正在绘制四条线,在它们之间形成一个区域,这是我试图填充的区域。 However, I can't quite figure it out due to the limited amount of arguments I can use to define the area.但是,由于可用于定义区域的参数数量有限,我无法完全弄清楚。

Any help would be greatly appreciated.任何帮助将不胜感激。

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

#global values

sigma_ct_inf = 0
sigma_ct_0 = 0
sigma_c_inf = 30
sigma_c_0 = 12
beta = 0.8

#values T 70
A = 359000
Wb = 40830202.33
Wt = 72079066.94
Mmin = 701.17
Mmax = 978.52


#Magnel Diagram
e = range(0, 1001)

disqualities = [1, 2, 3, 4] #'t70_top_0', 't70_top_inf', 't70_bot_0', 't70_bot_inf'

for i in disqualities:
    if i == 1:
        t70_top_0 = pd.DataFrame(e, columns = ['x'])
        t70_top_0['y'] = ((t70_top_0.x - (Wt/A))/((Mmin*10**6) + sigma_ct_0 * Wt))*10**6

    elif i == 2:
        t70_bot_0 = pd.DataFrame(e, columns = ['x'])
        t70_bot_0['y'] = ((t70_bot_0.x + (Wb/A))/((Mmin*10**6) + sigma_c_0 * Wb))*10**6

    elif i == 3:
        t70_top_inf = pd.DataFrame(e, columns = ['x'])
        t70_top_inf['y'] = (((t70_top_inf.x - (Wt/A))*beta)/((Mmax*10**6) - sigma_c_inf * Wt))*10**6

    elif i == 4:
        t70_bot_inf = pd.DataFrame(e, columns = ['x'])
        t70_bot_inf['y'] = (((t70_bot_inf.x + (Wb/A))*beta)/((Mmax*10**6) - sigma_ct_inf * Wb))*10**6


fig, ax = plt.subplots()
ax.set_title('Magnel Diagram, T-70')
line1 = ax.plot(t70_top_0['x'], t70_top_0['y'], lw = 0.5, label = 'Top, t = 0')
line1 = ax.plot(t70_bot_0['x'], t70_bot_0['y'], lw = 0.5,label = 'Bottom, t = 0')
line1 = ax.plot(t70_top_inf['x'], t70_top_inf['y'], lw = 0.5,label = 'Top, t = \u221E')
line1 = ax.plot(t70_bot_inf['x'], t70_bot_inf['y'], lw = 0.5,label = 'Bottom, t = \u221E')

plt.fill_between(t70_top_inf['x'], t70_top_inf['y'], t70_bot_inf['y'], where=t70_top_inf['y']<t70_bot_inf['y'], color = 'r', alpha = 0.4)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.ylabel('1/P0 [1/MN]')
plt.xlabel('Eccentricity [mm]')
plt.legend()
plt.show()

A new top curve can be defined as the minimum between the two existing top curves.新的顶部曲线可以定义为两条现有顶部曲线之间的最小值。 And a new bottom curve as the maximum between the existing bottom curves.并将新的底部曲线作为现有底部曲线之间的最大值。

The code below is somewhat simplified, using one common x-axis ('e').下面的代码有些简化,使用了一个通用的 x 轴 ('e')。 The naming is the same as from the question, where bottom and top seem to be switched.命名与问题相同,底部和顶部似乎已切换。

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

#global values
sigma_ct_inf = 0
sigma_ct_0 = 0
sigma_c_inf = 30
sigma_c_0 = 12
beta = 0.8

#values T 70
A = 359000
Wb = 40830202.33
Wt = 72079066.94
Mmin = 701.17
Mmax = 978.52

#Magnel Diagram
e = np.arange(0, 1001)

t70_top_0 = pd.Series({'y': ((e - (Wt / A)) / ((Mmin * 10 ** 6) + sigma_ct_0 * Wt)) * 10 ** 6})
t70_bot_0 = pd.Series({'y': ((e + (Wb / A)) / ((Mmin * 10 ** 6) + sigma_c_0 * Wb)) * 10 ** 6})
t70_top_inf = pd.Series({'y': (((e - (Wt / A)) * beta) / ((Mmax * 10 ** 6) - sigma_c_inf * Wt)) * 10 ** 6})
t70_bot_inf = pd.Series({'y': (((e + (Wb / A)) * beta) / ((Mmax * 10 ** 6) - sigma_ct_inf * Wb)) * 10 ** 6})

bot = np.min([t70_bot_0['y'], t70_bot_inf['y']], axis=0)
top = np.max([t70_top_0['y'], t70_top_inf['y']], axis=0)

fig, ax = plt.subplots()
ax.set_title('Magnel Diagram, T-70')
ax.plot(e, t70_top_0['y'], lw=0.5, label='Top, t = 0')
ax.plot(e, t70_bot_0['y'], lw=0.5, label='Bottom, t = 0')
ax.plot(e, t70_top_inf['y'], lw=0.5, label='Top, t = \u221E')
ax.plot(e, t70_bot_inf['y'], lw=0.5, label='Bottom, t = \u221E')

# ax.fill_between(e, t70_top_inf['y'], t70_bot_inf['y'], where=t70_top_inf['y'] < t70_bot_inf['y'], color='r', alpha=0.1)
# ax.fill_between(e, t70_top_0['y'], t70_bot_0['y'], where=t70_top_0['y'] < t70_bot_0['y'], color='b', alpha=0.1)
ax.fill_between(e, bot, top, where=top < bot, color='dodgerblue', alpha=0.4)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.ylabel('1/P0 [1/MN]')
plt.xlabel('Eccentricity [mm]')
plt.legend()
plt.show()

结果图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM