0

I am using Spyder Interface (Python 2.7) to numerically solve PDE's. I have the code set up to where U is calculated with respect to position, and time. U is a [nxm] matrix in my code where n is position and m is time. So at each U[n,m] the code gives a nth position at a mth time. Is there a way I can make use of a such matrix to make a mesh plot in python. I have searched, but for example numpy's mesh grid only deals with arrays. Thank you.

[[ 1.20643447  1.20520185  1.20397894 ...,  1.04589795  1.04587534 1.04585286]
[ 1.40901699  1.40658211  1.4041664  ...,  1.09172525  1.09168043 1.09163586]
[ 1.6039905   1.6004133   1.59686428 ...,  1.13741248  1.13734625 1.1372804 ]..., 
[ 2.3960095   2.3995867   2.40313572 ...,  2.54969453  2.55003659 2.55037764]
[ 2.59098301  2.59341789  2.57981471 ...,  2.59750546  2.59785406 2.59820163]
[ 2.79356553  2.74473913  2.71231633 ...,  2.64640578  2.64675767 2.64710852]]

These are the many values for U the shell spits at me. As you can see I would be dealing with 600 different arrays because the matrix is set up to find U at at specific time and position. Taking a total of 600 time steps. This is a example of a type of graph that I am trying to reproduce.

Gregory Price
  • 89
  • 1
  • 2
  • 12
  • 1
    Is there a reason you can't, prior to plotting, convert the matrix to an array, with `np.asarray(U)` or `np.array(U)`? – EFT Jun 27 '17 at 17:23

1 Answers1

6

Please try to use the search function; you can find working code here among many other threads on plotting surfaces here on Overflow.


I think you might have some terminology mix up here. You have a matrix of values which correspond to the values of a function of two dimensions; this is not a meshgrid. In matplotlib you can visualize a three-dimensional matrix in a number of ways, typically as a surface, a wireframe, or an image (as a heatmap).

The function np.meshgrid() gives you N-dimensional indices. You need this for your n and m vectors, not your matrix U, to turn them into multidimensional arrays the same shape as your matrix. Luckily for you, matplotlib doesn't care whether U is a matrix or a vector.

For example with np.meshgrid():

>>> t = np.linspace(0,60,6000)
>>> x = np.linspace(0,2*np.pi,3600)
>>> T, X = np.meshgrid(t, x)
>>> t.shape
(6000,)
>>> x.shape
(3600,)
>>> T.shape
(3600, 6000)
>>> X.shape
(3600, 6000)

And now to create a function U(x,t):

>>> U = np.matrix(x[:, np.newaxis] * t) # broadcast
>>> U.shape
(3600, 6000)

And to plot the surface, you can use the surf function in Axes3D from matplotlib for example, but you can also use the wireframe or image method linked above.

>>> import matplotlib.pyplot as plt
>>> import pylab
>>> from mpl_toolkits.mplot3d import Axes3D

>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> surf = ax.plot_surface(X,T,U)
>>> plt.show()

Surface plot

alkasm
  • 22,094
  • 5
  • 78
  • 94
  • So in my case. I can just put U into the 3-D surface plot with pyplot even though it is a 2-D array it should still work? Because after setting my mesh for X and T I got a shape mismatch value error. When including U. – Gregory Price Jun 27 '17 at 19:28
  • You probably just have `X` and `T` backwards in your array---I wasn't being particularly faithful to your exact array. Just print the shapes out as you go like I did, and you'll get more comfortable with the relationships between the numpy shapes. It's a little confusing at first, but mostly intuitive. You want the meshgrids to be the same shape as your matrix. – alkasm Jun 27 '17 at 19:32
  • You were right I checked the shapes of U, X, and T and they did not work properly. Then I changed the meshgrid format, and it worked perfectly. Thanks for your help. – Gregory Price Jun 27 '17 at 19:36