2

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?

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. Commented Mar 6, 2019 at 10:22
  • Btw. same question here. I cannot close as duplicate because the answer does not have any upvotes. Commented Mar 6, 2019 at 11:20

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.