简体   繁体   English

具有不同数组大小的Python 3D图

[英]Python 3D plot with different array sizes

I have been using Matlab for a few years which is quite easy (in my opinion) and powerful when it comes to 3D-plots such as surf , contour or contourf . 我已经使用Matlab几年了(在我看来)相当容易,并且在涉及到3D图形(例如surfcontourcontourf )时功能强大。 It seems at least more unintuitive to me to do the same in Python. 对我来说,在Python中执行相同的操作至少似乎更不直观。

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0,100,0.1) # time domain
sp = np.arange(0,50,0.2) # spatial domain
c = 0.5
u0 = np.exp(-(sp-5)**2)  
u = np.empty((len(t),len(sp))
for i in range(0,len(t)):
  u[i][:] = u0*(sp-c*t)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(t,sp,u)
plt.show()

So, in Matlab it would be that easy I think. 因此,在Matlab中,我认为就这么容易。 What do I have to do in order to get a 3D-Plot (surface or whatever) with two arrays for the x and y dimensions with different sizes and a z-matrix giving a value to each grid point? 为了获得3D绘图(表面或其他任何东西),我该怎么办,该绘图具有两个分别用于x和y尺寸的数组,且两个数组的大小不同,而z矩阵为每个栅格点赋予一个值?

As this is a basic question, feel free to explain a bit more or just give me a link with an answer. 由于这是一个基本问题,请多解释一点,或者给我一个带有答案的链接。 Unfortunately, I do not really understand what is happening in the codes I read regarding this problem so far. 不幸的是,到目前为止,我并不真正理解我阅读的代码中发生的有关此问题的情况。

I don't think what you have written would work in matlab either (I may be wrong, I haven't used it in a while). 我认为您编写的内容也不会在Matlab中运行(我可能是错的,我有一段时间没有使用它了)。

To do a plot_surface(X, Y, Z) , X, Y, Z must be 2D arrays of equal size. 要执行plot_surface(X, Y, Z)X, Y, Z必须是大小相等的2D数组。 So, just like you would do in matlab: 因此,就像在matlab中一样:

T, SP = numpy.meshgrid(t, sp)
plot_surface(T, SP, u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM