1

So, I've got three arrays of data, X, Y, and Z, each 225 numbers long. What I would like to do is to plot all three values at the same time on a surface plot. When I try to use

ax.plot_surface(X,Y,Z)

it tells me that Z needs to be a 2D array. I've looked it up and I've seen that it's possible to plot Z if it was a function of X and Y, but I need the first Z point to be associated with the first X and Y point etc. Is this possible in Python?

Peter Read
  • 11
  • 1
  • 2
  • In the required `Z` array, the value at `Z[i,j]` is associated with the coordinates `X[i,j]` and `Y[i,j]`. The best way to create those arrays depends on your data. If it is already gridded in some way, it might be enough to reshape them. – ImportanceOfBeingErnest Mar 06 '19 at 10:22
  • Btw. same question [here](https://stackoverflow.com/questions/44355270/plot-3d-in-python-using-three-lists/44355400#44355400). I cannot close as duplicate because the answer does not have any upvotes. – ImportanceOfBeingErnest Mar 06 '19 at 11:20

2 Answers2

1

So, I've got three arrays of data, X, Y, and Z, each 225 numbers long. What I would like to do is to plot all three values at the same time on a surface plot.

So, from what i understood you want to plot a 3d surface plot. But it seems you are only providing 3 1xn arrays. (in this case n == 255)

When plotting a surface plot, what you are doing in practice is getting each and every possible combination of a base (XY plane) and telling how high is a point Z on that given XY coordinates, hence Z is depicted as a function Z(i,j)

but I need the first Z point to be associated with the first X and Y point etc. Is this possible in Python?

Yes, but if you associate each Z point to the first X,Y and so on, you would only have the Z values for X==Y, which would be incomplete information for your surfaceplot!

A good (great) example of surface plot comes from matplotlib official docs

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

which results in:

enter image description here

What the code is actually doing:

  • Defining the input vectors X and Y (both range and interval)
  • Making a meshgrid out of those vectors (if unclear as to what a meshgrid is, print the output!)
  • Defining a function over the X,Y domain
  • Applying it to get Z

If you check, X,Y and Z are 2 dimensional arrays!

Hope it helps!

epattaro
  • 2,330
  • 1
  • 16
  • 29
1

If your arrays are all 1-D, then I think what you want is

ax.plot_trisurf(X,Y,Z, triangles=tri.triangles, cmap=plt.cm.Spectral)

See more info at https://matplotlib.org/examples/mplot3d/trisurf3d_demo2.html

NAP_time
  • 181
  • 9