类型错误:使用 shading='flat' 时,C (50, 49) 的尺寸应比 X(50) 和 Y(50) 小 1,请参阅帮助(pcolormesh)

问题描述 投票:0回答:1

我的输出 T_results 和 c_results 最后的值为 0,这是我不想要的,所以我对这些数组进行了切片(不包括它们的最后一列),并得到了它们各自的数组

T_results_cleaned = T_results[:, :-1]
c_results_cleaned = c_results[:, :-1]
。但是,现在当我使用这些切片数组进行绘图时,我收到以下错误消息
TypeError: Dimensions of C (50, 49) should be one smaller than X(50) and Y(50) while using shading='flat' see help(pcolormesh)

下面是我的完整代码,它使用有限差分求解一组方程。


import numpy as np
import matplotlib.pyplot as plt
import time 
import pandas as pd
nz = 50
dz = 1 / nz
nt = 50
t = np.linspace(0, 4, nt)

p = np.array([0.01, 0.5, 1.0])

# Initialize concentration and temperature arrays
C = np.zeros(nz)
T = np.zeros(nz)

# Set initial condition for concentration
C[0] = 1.0

# Initialize arrays to store results
c_results = np.zeros((nt, nz))
T_results = np.zeros((nt, nz))

# Define the Arrhenius rate constant function
def k(temperature):
    arrh_expr = 4.68 * np.exp(-806 / ((600 - 273) * temperature + 273))
    return arrh_expr

# Calculate time derivative at specific spatial points
def rhsC(c0, c1, c2, T1, dz, p):
    return (p[0] * (c2 - 2 * c1 + c0) / dz**2 - 
            p[1] * (c2 - c0) / (2 * dz) - 
            p[2] * k(T1) * c1**2)

def rhsT(T0, T1, T2, c1, dz, p):
    return (0.01 * (T2 - 2 * T1 + T0) / dz**2 - 
            0.5 * (T2 - T0) / (2 * dz) + 
            2.45 * k(T1) * c1**2)

# Time-stepping loop
for it in range(nt):
    c_results[it, :] = C
    T_results[it, :] = T

    T_results_cleaned = T_results[:, :-1] #select all rows and all columns except the last column
    c_results_cleaned = c_results[:, :-1] #select all rows and all columns except the last column
    
    for iz in range(1, nz - 1):
        # Calculate concentration and temperature derivatives
        dc_dt = rhsC(C[iz - 1], C[iz], C[iz + 1], T[iz], dz, p)
        dT_dt = rhsT(T[iz - 1], T[iz], T[iz + 1], C[iz], dz, p)
        
        # Update concentration and temperature using forward Euler
        C[iz] += dz * dc_dt
        T[iz] += dz * dT_dt

# Create a mesh for plotting
z = np.linspace(0, 1, nz)

# Plot concentration and temperature profiles
plt.pcolormesh(z, t, c_results_cleaned)
plt.colorbar(label='Concentration')
plt.xlabel('z')
plt.ylabel('t')
plt.title('Concentration Profile')
plt.show()

plt.pcolormesh(z, t, T_results_cleaned)
plt.colorbar(label='Temperature')
plt.xlabel('z')
plt.ylabel('t')
plt.title('Temperature Profile')
plt.show()

我也尝试在 plt 模块中对 z 和 t 数组进行切片,

plt.pcolormesh(z[:-1], t[:-1], c_results_cleaned)
以匹配尺寸,但效果不佳。我该如何解决这个问题?

python numpy numpy-ndarray
1个回答
0
投票

为了扩展评论,如果我正确理解你的问题,如果使用完整的数据,该图将起作用:

plt.pcolormesh(z, t, c_results)

但据我了解,您关心的是末尾的零列。如果您必须删除它,您还需要从

z
变量中删除一行,如下所示:

plt.pcolormesh(z[:-1], t, c_results_cleaned)

要得到这样的情节:

© www.soinside.com 2019 - 2024. All rights reserved.