3D graph with python matplotlib 2D Array

I have 2 1D arrays with x and y values, as well as a 2D array with z values ​​for each point where the columns correspond to x values ​​and the rows correspond to y values. Is there any way to get plot_surface with this data? when I try to do this, he does not return me any conspiracy. Here is the code: (calculate_R is the function I made for the program)

x=np.arange(0,10,1)
y=np.arange(0,1,0.2)
lx= len(x)
ly=len(y)

z=np.zeros((lx,ly))

for i in range(lx):
    for j in range(ly):
        z[i,j]=calculate_R(y[j],x[i])

fig = plt.figure()
ax = Axes3D(fig)
x, y = np.meshgrid(x, y)
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='hot')
+4
source share
1 answer

You forgot to call plt.show()to display your schedule.

Note that you can use numpy vectorization to speed up the calculation z:

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

x = np.arange(0,10,1)
y = np.arange(0,1,0.2)

xs, ys = np.meshgrid(x, y)
# z = calculate_R(xs, ys)
zs = xs**2 + ys**2

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xs, ys, zs, rstride=1, cstride=1, cmap='hot')
plt.show()

, .

+5

Source: https://habr.com/ru/post/1535541/


All Articles