如何在Matplotlib中仅绘制T_f函数340-373K范围的数值?
问题:仅绘制T_f函数340-373K范围的数值
我尝试绘制函数T_f的特定范围数值,但不知如何操作。我使用np.arange生成了T_f方程所需的z数组,希望仅绘制x轴340-373K范围的数值。以下是我的代码:
import numpy as np import matplotlib.pyplot as plt case = int(input('Which Case [1 (PWR) or 2 (BWR)]? ')) if case == 1: # PWR H = 3.80 # m Lc = 3.80 # m D_rod = 0.0095 # m pitch = 0.0125 # m G = 3460 # kg/m^2-s q_0 = 33 # kW/m - Linear heat rate P_0 = 15 # MPa - Initial pressure T_f_in = 551 # Kelvin - Inlet temperature T_sat = 373 # Kelvin cp = 4.22 # kJ/kg hfg = 2256.4 # kJ/kg g = 9.81 # m/s^2 heated_parameter = np.pi * D_rod # m area = pitch ** 2 - 0.25 * np.pi * D_rod ** 2 # m^2 volume = 0.5 * np.pi * D_rod ** 2 * H # m^3 circumference = 2 * np.pi * (D_rod / 2) # m rho_f = 958.4 # kg/m^3 - Water density mu_f = 0.00000000009888 # MPa/s - Water viscosity Re_L = (rho_f * volume * Lc) / mu_f # Reynold's number f_1phaseL = 0.316 * Re_L ** -0.25 # Friction factor z = np.arange(0, 3.8, 0.001) Xe = -q_0 * heated_parameter / (G * circumference * area * hfg) * H / np.pi * np.cos(np.pi * z / H) + cp * (T_sat - T_f_in) / hfg #plt.plot(Xe, z, label="Equilibrium Quality") void = z*0 #plt.plot(void, z, label="Void Fraction") X = z * 0 #plt.plot(X, z, label="Quality") #plt.xlabel("Void Fraction, Equilibrium Quality, Quality") P = P_0 - ((heated_parameter / area * 0.5 * f_1phaseL * G ** 2 * z / rho_f) + rho_f * g * z) * 0.000001 #plt.plot(P, z) #plt.xlabel("Pressure (MPa)") plt.vlines(x=373, ymin=0, ymax=3.8, label="Saturated Temperature", colors='Red') T_f = T_sat - (q_0 * heated_parameter * H / (cp * circumference * G * area * np.pi) * np.cos(np.pi * z / H)) plt.plot(T_f, z, label="Fluid Temperature") plt.xlabel("Temperature (K)") plt.legend() plt.ylabel("z (meters)") plt.show()
解决方法
要仅绘制T_f在340-373K范围的数值,核心是通过布尔索引筛选出符合温度范围的T_f和对应的z值,再进行绘制:
- 创建过滤掩码:生成布尔数组标记
T_f在目标范围内的位置
mask = (T_f >= 340) & (T_f <= 373)
- 用过滤后的数据绘图:替换原绘图语句,使用掩码筛选后的数组
plt.plot(T_f[mask], z[mask], label="Fluid Temperature (340-373K)")
- 可选:锁定x轴范围:确保图表仅展示340-373K区间,避免无关数据干扰
plt.xlim(340, 373)
修改后的完整代码
import numpy as np import matplotlib.pyplot as plt case = int(input('Which Case [1 (PWR) or 2 (BWR)]? ')) if case == 1: # PWR H = 3.80 # m Lc = 3.80 # m D_rod = 0.0095 # m pitch = 0.0125 # m G = 3460 # kg/m^2-s q_0 = 33 # kW/m - Linear heat rate P_0 = 15 # MPa - Initial pressure T_f_in = 551 # Kelvin - Inlet temperature T_sat = 373 # Kelvin cp = 4.22 # kJ/kg hfg = 2256.4 # kJ/kg g = 9.81 # m/s^2 heated_parameter = np.pi * D_rod # m area = pitch ** 2 - 0.25 * np.pi * D_rod ** 2 # m^2 volume = 0.5 * np.pi * D_rod ** 2 * H # m^3 circumference = 2 * np.pi * (D_rod / 2) # m rho_f = 958.4 # kg/m^3 - Water density mu_f = 0.00000000009888 # MPa/s - Water viscosity Re_L = (rho_f * volume * Lc) / mu_f # Reynold's number f_1phaseL = 0.316 * Re_L ** -0.25 # Friction factor z = np.arange(0, 3.8, 0.001) Xe = -q_0 * heated_parameter / (G * circumference * area * hfg) * H / np.pi * np.cos(np.pi * z / H) + cp * (T_sat - T_f_in) / hfg #plt.plot(Xe, z, label="Equilibrium Quality") void = z*0 #plt.plot(void, z, label="Void Fraction") X = z * 0 #plt.plot(X, z, label="Quality") #plt.xlabel("Void Fraction, Equilibrium Quality, Quality") P = P_0 - ((heated_parameter / area * 0.5 * f_1phaseL * G ** 2 * z / rho_f) + rho_f * g * z) * 0.000001 #plt.plot(P, z) #plt.xlabel("Pressure (MPa)") plt.vlines(x=373, ymin=0, ymax=3.8, label="Saturated Temperature", colors='Red') T_f = T_sat - (q_0 * heated_parameter * H / (cp * circumference * G * area * np.pi) * np.cos(np.pi * z / H)) # 筛选340-373K范围的数据 mask = (T_f >= 340) & (T_f <= 373) plt.plot(T_f[mask], z[mask], label="Fluid Temperature (340-373K)") # 锁定x轴显示范围 plt.xlim(340, 373) plt.xlabel("Temperature (K)") plt.legend() plt.ylabel("z (meters)") plt.show()
内容的提问来源于stack exchange,提问作者Joseph Wunschel
相关产品推荐
相关产品推荐

