Matplotlib fill_between() 多行
Matplotlib fill_between() Multiple lines
我正在绘制四条线,它们之间形成一个区域,这是我要填充的区域。但是,由于我可以用来定义区域的参数数量有限,我不太明白。
如有任何帮助,我们将不胜感激。
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()
可以将新的顶部曲线定义为两条现有顶部曲线之间的最小值。并且新的底部曲线作为现有底部曲线之间的最大值。
下面的代码有些简化,使用一个通用的 x 轴 ('e')。命名与问题相同,底部和顶部似乎已切换。
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()
我正在绘制四条线,它们之间形成一个区域,这是我要填充的区域。但是,由于我可以用来定义区域的参数数量有限,我不太明白。
如有任何帮助,我们将不胜感激。
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()
可以将新的顶部曲线定义为两条现有顶部曲线之间的最小值。并且新的底部曲线作为现有底部曲线之间的最大值。
下面的代码有些简化,使用一个通用的 x 轴 ('e')。命名与问题相同,底部和顶部似乎已切换。
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()