简体   繁体   中英

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 . It seems at least more unintuitive to me to do the same in 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. 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?

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).

To do a plot_surface(X, Y, Z) , X, Y, Z must be 2D arrays of equal size. So, just like you would do in matlab:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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