Issue
I am trying to plot a 3D chart, where my Z values would be assigned to each [X,Y] ordered pair. For example, these are my X, Y and Z values:
X = [1,2,3,4,5]
Y = [1,2,3,4,5]
Z = [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,]
And the Z values, correspond to the following [X,Y] ordered pair:
Z = [X[0]Y[0], X[0]Y[1], X[0]Y[2],...., X[5]Y[4], X[5]Y[5]]
Thank you!!
Solution
you can do it using np.meshgrid
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X = [1,2,3,4,5]
Y = [1,2,3,4,5]
Z = [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,]
xy = np.array(np.meshgrid(X,Y)).reshape(-1,2)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(xy[:,0],xy[:,1],Z)
plt.show()
you can also use scatter
ax.scatter(xy[:,0],xy[:,1],Z)
for surface plot
x , y = np.meshgrid(X,Y)
ax.plot_surface(x,y,np.array(Z).reshape(5,5))
plt.show()
Answered By - Aly Hosny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.