3

I need to create 3d surface plot the dependence of the area on two parameters (A and C). The function that returns the area of graph is called get_square(param_a, param_c) i have 2 arrays with params C and A

Python 3.5 I got ValueError: Argument Z must be 2-dimensional. I don't understand what i need to do. I tried many different way but every time i had fail

a = [70, 71,72,73,74,75,76,77,78,79,80,81,82,82,83,84,85,86,87,88,89]
c = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
result_area = []
for i in range(len(a)):
    result_area.append(get_area(a=a[i], c=c[i])
# in my case result_area is  [8.37489748679822,
 8.414865734605842,
 8.425909102940215,
 8.409343482533345,
 8.36477993434033,
 8.291545577425058,
 8.19076737834649,
 8.060984569963367,
 7.899169690316133,
 7.709739062145273,
 7.483527412013634,
 7.22563128509485,
 6.923664590863622,
 6.579039142980158,
 6.180241810498949,
 5.723812526395248,
 5.195589966465734,
 4.561306845225758,
 3.7683571369877655,
 2.696998299888183]

# then i try to create 3d surface:

from mpl_toolkits.mplot3d import axes3d

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
a = np.array(a)
c = np.array(c)
result = np.array(result_area )

ax.plot_surface(a, c, result_area , rstride=10, cstride=10)

plt.show()

I expect 3d surface plot like there https://matplotlib.org/mpl_examples/mplot3d/surface3d_demo3.png (Of course, I understand that since I have a graph of the dependence of the area on two parameters, the surface will be different)

Denis
  • 75
  • 1
  • 7

1 Answers1

1

Try this - for x, y and z you need matrices to plot. Specifically for your z axis you need a matrix so that each combination of x,y is mapped to a z coordinate.

from mpl_toolkits.mplot3d import axes3d

x = [70, 71,72,73,74,75,76,77,78,79,80,81,82,82,83,84,85,86,87,88]
y = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

def get_area(x, y):
    return x+y

x = np.array(x)
y = np.array(y)
X, Y = np.meshgrid(x, y)  # creates 2 matrices: one repeats x, the other repeats y
Z = get_area(X, Y)

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z , rstride=10, cstride=10)
plt.show()

See here: surface plots in matplotlib

Kai Aeberli
  • 1,189
  • 8
  • 21