# 3-dimensional plotting with nonuniform grids

I have 3 variables I am considering: time (t), 1-dimensional space (x), and intensity (I). I would like to plot the intensity in the z-axis as a function of t and x (the latter two variables would form an xy-plane). With the current data I have, these variables themselves are 2-dimensional arrays where each corresponding index from the different arrays match appropriately to a single datapoint (i.e. (x,t,I) = (x[k,i],t[k,i],I[k,i])). (Ideally this approach would be extended to arrays of higher dimensions than two.)

Here is an example set of data:

I = [[  10.55   0.   0.]
[  0.   0.   0.01]
[  0.2   -0.1   3.33]
[  0.  2.14   0.]
[  0.   3.80   0.]
[  9.02   0.   0.]]

t = [[   0.  400.  1000.]
[   1.  300.  800.]
[   0.  500.  900.]
[   200.  400.  0.]
[   100.  700.  0.]
[   0.  0.    0.]]

x = [[    0.     0.     0.]
[ 500.  1500.  1500.]
[ 3000.  3000.  8000.]
[ 3300.  4500.  0.]
[ 6000.  6000.  0.]
[ 7500.  0.     0.]]


Just a note: the lengths of each row have been made equivalent in the arrays, but they should ideally be null if z[k,i] = t[k,i] = 0. if k != 0 and i != 0. As you can tell, in the example above, there are 4 such elements corresponding to the indices: [k,i] = [3,2], [4,2], [5,1], [5,2]. Although these would only affect the coordinate (0,0,0).

To restate my goal: I would like to plot t, x, and I as a 3-dimensional surface. In the past, using code such as:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(t,x)
surf = ax.plot_surface(X,Y,I, rstride=4, cstride=4, alpha=0.1)


worked for cases where x and t were 1-dimensional arrays. But I'm curious as to any good approaches to modelling the example data I've shown above as a 3-dimensional surface where there is a nonuniform grid.

• I think that this post should be migrated to Stack Overflow. – nicoguaro Aug 4 '16 at 3:55

Your data is already in the right shape, then you don't need to create a meshgrid. See the code below

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

I = np.array([
[10.55, 0., 0.],
[0., 0., 0.01],
[0.2, -0.1, 3.33],
[0., 2.14, 0.],
[0., 3.80, 0.],
[9.02, 0., 0.]])

t = np.array([
[0., 400., 1000.],
[1., 300., 800.],
[0., 500., 900.],
[200., 400., 0.],
[100., 700., 0.],
[0., 0., 0.]])

x = np.array([
[ 0.,  0.,  0.],
[ 500., 1500., 1500.],
[ 3000., 3000., 8000.],
[ 3300., 4500., 0.],
[ 6000., 6000., 0.],
[ 7500., 0.,  0.]])

fig = plt.figure()