[英]Matplotlib - Plot 3D with for loop
I want to plot several 3D points with matplotlib. 我想用matplotlib绘制几个3D点。 My coordinates are stored in 2D arrays because i got multiple cases and so i would like to plot all the cases in a same 3D plot with a "for loop" but when i do that, the results appeared on different plots...
我的坐标存储在2D数组中,因为我有多个个案,因此我想用“ for循环”在同一3D绘图中绘制所有个案,但是当我这样做时,结果出现在不同的绘图中...
As example : 例如:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
X = np.array([[3,2,1],[4,5,6]])
Y = np.array([[1,2,1],[2,3,4]])
Z = np.array([[10,11,12],[13,12,16]])
for i in range(0,X.shape[0]):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[i,:], Y[i,:], Z[i,:], c='r', marker='o')
ax.set_xlabel('Z')
ax.set_ylabel('X')
ax.set_zlabel('Y')
plt.show()
You create a new figure each iteration and plot it each iteration. 您可以在每次迭代中创建一个新图形,并在每次迭代中对其进行绘制。 Also you always create the first suplot of a 1x1 subplot-grid.
同样,您始终会创建1x1子图网格的第一个支持。
You probably want a x.shape[0] x 1
grid or 1 x x.shape[0]
grid: 您可能需要一个
x.shape[0] x 1
网格或1 x x.shape[0]
网格:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
X = np.array([[3,2,1],[4,5,6]])
Y = np.array([[1,2,1],[2,3,4]])
Z = np.array([[10,11,12],[13,12,16]])
# Create figure outside the loop
fig = plt.figure()
for i in range(0,X.shape[0]):
# Add the i+1 subplot of the x.shape[0] x 1 grid
ax = fig.add_subplot(X.shape[0], 1, i+1, projection='3d')
ax.scatter(X[i,:], Y[i,:], Z[i,:], c='r', marker='o')
ax.set_xlabel('Z')
ax.set_ylabel('X')
ax.set_zlabel('Y')
# Show it outside the loop
plt.show()
If you want to plot them all into the same plot use: 如果要将它们全部绘制到同一图中,请使用:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.set_xlabel('Z')
ax.set_ylabel('X')
ax.set_zlabel('Y')
for i in range(0,X.shape[0]):
# Only do the scatter inside the loop
ax.scatter(X[i,:], Y[i,:], Z[i,:], c='r', marker='o')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.