Effacer les filtres
Effacer les filtres

Troubling to convert a 3d plot from Matlab to Python!

22 vues (au cours des 30 derniers jours)
Nguyen Phuong Hung
Nguyen Phuong Hung le 24 Nov 2020
Réponse apportée : surya venu le 17 Juil 2024 à 5:31
Hi everyone,
I have a problem in transforming a 3D plot from Matlab to Python. Here are my Matlab and Python codes. The output in Python is "Argument Z must be 2-dimensional." I still do not understand the framework in Matlab working in creating such a plot like this. Nothing that a T matrix has the shape (15, 25, 650). Are there differences between mesh in Matlab and np.meshgrid in Python? I would love it if there are ideas to help me solve this problem. Thank you!
#Matlab Code
m=25; n=15;
x(1)=0;
for i=2:m+1
dx=200;
x(i)=x(i-1)+dx;
end
y(1)=0;
for j=2:n+1
dy=100;
y(j)=y(j-1)+dy;
end
%Central mesh position
for j=1:n
h(j)=(y(j)+y(j+1))/2;
end
for i=1:m
z(i)=(x(i)+x(i+1))/2;
end
% Boundary condition
for j=1:n
for i=1:m
T(j,i,1)=0;
end
end
mesh(z,h,T(:,:,1));
Python code
m = 25
n = 15
tmax = 650
x = np.zeros(m);
y = np.zeros(n);
h = np.zeros(n);
z = np.zeros(m);
T = np.zeros((n,m,tmax))
x[0] = 0
for i in range(1, m):
x[i] = x[i-1] + 200
y[0] = 0
for j in range(1, n):
y[j] = y[j-1] + 100
for j in range(1, n):
h[j] = (y[j-1] + y[j])/2
for i in range(1, m):
z[i] = (x[i] + x[i-1])/2
for j in range(1, n):
for i in range(1, m):
T[j, i, 1] = 0
X, Y, Z = np.meshgrid(z, h, T)
fig = plt.figure(figsize=(14,8))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

Réponses (1)

surya venu
surya venu le 17 Juil 2024 à 5:31
Hi,
The error you're encountering arises because "np.meshgrid" in Python and "mesh" in MATLAB have some differences in how they handle dimensions and inputs. In your case, "Z" should indeed be a 2-dimensional array for the "plot_surface" function in Matplotlib.
Here's a corrected version of your Python code that should work:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
m = 25
n = 15
tmax = 650
x = np.zeros(m)
y = np.zeros(n)
h = np.zeros(n)
z = np.zeros(m)
T = np.zeros((n, m, tmax))
x[0] = 0
for i in range(1, m):
x[i] = x[i-1] + 200
y[0] = 0
for j in range(1, n):
y[j] = y[j-1] + 100
for j in range(n):
h[j] = (y[j] + y[j-1])/2 if j > 0 else y[j]/2
for i in range(m):
z[i] = (x[i] + x[i-1])/2 if i > 0 else x[i]/2
for j in range(n):
for i in range(m):
T[j, i, 0] = 0 # Note: Use 0 instead of 1 for the first time step
# Create the meshgrid
X, Y = np.meshgrid(z, h)
# Extract the first time slice of T
Z = T[:, :, 0]
fig = plt.figure(figsize=(14, 8))
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Explanation of Differences:
MATLAB "mesh":
  • In MATLAB, mesh(X, Y, Z) assumes X, Y, and Z are 2D matrices that define the grid and the surface height.
To know more about "mesh" function, check out:
Python "np.meshgrid":
  • "np.meshgrid" in Python creates coordinate matrices from coordinate vectors. The resulting X and Y are 2D arrays suitable for surface plotting.
To know more about "meshgrid" function, check out:
Hope it helps

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by