1

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.

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I think it both works in Matlab. However, I tried to follow your approach and it turned out that I had to change T, SP = np.meshgrid(t,sp) to SP,T = np.meshgrid(sp,t) although I use T as x-domain. Do you know the reason for that?
T, SP = np.meshgrid(t,sp) and SP,T = np.meshgrid(sp,t) should be equivalent, as meshgrid outputs are in the same order as inputs. What's the error you are getting??
"incompatible dimensions on axis %r." % (axis,)) ValueError: shape mismatch: two or more arrays have incompatible dimensions on axis 0. The difference is that your approach gives sp.shape = x,y while the other approach gives sp.shape = y,x while u.shape =x,y. However, that is at least unintuitive to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.