numpy 使用MatPlotLib绘制二维彩色图

vwoqyblh  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(82)

有人能帮我用MatPlotLib生成一个2D彩色图吗?我试图绘制一个函数removal_fraction(P_inlet,T_inlet,P_tank),其中P_tank是常数。
下面是我的代码:

def removal_fraction(P_inlet,T_inlet,P_tank):
    #Pressure in flash tank is P_tank, in Pa(a)
    
    #Enthalpy of sap before flash tank
    H_inlet=cp.PropsSI('H',"P",P_inlet,"T",T_inlet,"water") #J/kg

    #Sensible enthalpy of sap once in the tank
    H_sensible_tank=cp.PropsSI('H',"P",P_tank,"Q",0,"water") #J/kg

    #Heat lost to latent vaporization in the tank
    Latent_heat_transferred=H_inlet-H_sensible_tank #J/kg

    #Latent enthalpy of vaporization of sap in the tank (J/kg)
    H_latent_tank=cp.PropsSI('H',"P",P_tank,"Q",1,"water")-cp.PropsSI('H',"P",P_tank,"Q",0,"water") #J/kg

    #Proportion of water evaporated
    R_water_evap=Latent_heat_transferred/H_latent_tank #kg water/kg water
    return R_water_evap

P_inlet=np.linspace(200000,100000*40,500) #Pa(a)
T_inlet=np.linspace(90+273.15,200+273.15,500) #K
P_tank=(14.7-10)*6894.76 #Pa(a)

#Creating the 2D grid
P_inlet_vals,T_inlet_vals=np.meshgrid(P_inlet,T_inlet,indexing='ij')
Removal_fraction=np.zeros((len(P_inlet_vals),len(T_inlet_vals)))
#Computing removal_fraction for each point in the grid
for i in range(len(P_inlet)):
    for j in range(len(T_inlet)):
        Removal_fraction[i,j]=removal_fraction(P_inlet_vals[i,j],T_inlet_vals[i,j],P_tank)

字符串
我想绘制的是去除率作为P_inlet和T_inlet的函数。我在制作去除率数组时遇到了困难。Propssi函数不会接受数组。所以我必须使用for循环来传递每个值。我不确定如何构造这个数组才能在2D彩色图上正确绘制。

gg0vcinb

gg0vcinb1#

要将其绘制为2D网格,您需要将Pinlet和Tinlet“扩展”为2D网格,然后计算该网格上每个点的removal_fraction。示例如下。(根据@科尔内利亚的建议,更新为使用等高线图而不是imshow)。
x1c 0d1x的数据

import numpy as np

#Generate some test data
p_inlet = np.linspace(200000, 100000 * 40, 500) #Pa(a)
t_inlet = np.linspace(90 + 273.15, 200 + 273.15, 500) #K
p_tank = 0.6

#Create a grid out of the x and y values
p_inlet_grid, t_inlet_grid = np.meshgrid(p_inlet, t_inlet)

#Compute removal_frac for each point in the grid
removal_frac = p_inlet_grid / t_inlet_grid * np.sinc(t_inlet_grid / t_inlet_grid.max()**0.6)

#
# Plot
#

#plt.imshow() is one way, but I'll use .contourf() and .contour()
# plt.imshow(removal_frac, cmap='plasma', interpolation='none', extents=<set bounds to match data>)

#Contour filled plot. Increase levels= as you prefer
plt.contourf(p_inlet_grid, t_inlet_grid, removal_frac, levels=25, cmap='plasma')
plt.colorbar()

#Optionally overlay contour lines
#You could also use contour independently, making a plot out of the lines only
contours = plt.contour(p_inlet_grid, t_inlet_grid, removal_frac,
                       levels=7, colors='black', alpha=0.8)
plt.clabel(contours, inline=True, fontsize=8)

plt.xlabel('$P^{inlet}$', fontsize=14)
plt.ylabel('$T^{inlet}$', fontsize=14)
plt.gcf().set_size_inches(6, 4)
plt.title(f'Removal fraction when Ptank={p_tank}')

plt.show()

字符串
在3D中:


ax = plt.figure().add_subplot(projection='3d')

ax.plot_surface(p_inlet_grid, t_inlet_grid, removal_frac, cmap='plasma')
ax.set_xlabel('Pinlet')
ax.set_ylabel('Tinlet')


修改OP的代码,其中计算在循环中发生:

#Creating the 2D grid
P_inlet_vals, T_inlet_vals = np.meshgrid(P_inlet, T_inlet)
Removal_fraction = np.zeros_like(P_inlet_vals)

#Computing removal_fraction for each point in the grid
for row in range(len(T_inlet)):
    for col in range(len(P_inlet)):
        Removal_fraction[row, col] = removal_fraction(
            P_inlet_vals[row, col], T_inlet_vals[row, col], P_tank
        )

iqih9akk

iqih9akk2#

你的变量Removal_fractions包含一个长度为500的向量,而不是一个二维数组。你的H_inlet也是一个长度为500的向量,而不是一个500x500的矩阵。你必须在所有压力和温度的组合上创建一个循环。

#Enthalpy of sap before flash tank
    H_inlet = []
    for P in P_inlet:
        H_inlet.append(cp.PropsSI('H',"P",P,"T",T_inlet,"water")) #J/kg

字符串

相关问题