Python:如何在3D子图中切换外轴

时间:2018-01-04 16:41:42

标签: python matplotlib

我正试图并排绘制两个3D图。但是我的结果如下:

enter image description here

我想删除外轴(来自[0..1]的那些)

到目前为止我尝试过的所有内容

ax.axis('off')

ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])

仅删除了我想保留的内轴(3D坐标系的内轴)。

到目前为止,这是我的代码:

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

# Generate coordinates for scatter plot
x, y, z = np.meshgrid(np.arange(data1.shape[0]), np.arange(data1.shape[1]), np.arange(data1.shape[2]))
[x, y, z] = (np.reshape(x, (-1)), np.reshape(y, (-1)), np.reshape(z, (-1)))

# Generate scatter plot
numRows = 1
numCols = 2
fig, axes = plt.subplots(numRows, numCols, figsize=(20, 12))
ax = fig.add_subplot(121, projection='3d')
scat = ax.scatter(x, y, z, c=np.reshape(data1, (-1)), cmap='jet', marker="s")
ax = fig.add_subplot(122, projection='3d')
scat = ax.scatter(x, y, z, c=np.reshape(data2, (-1)), cmap='jet', marker="s")
plt.show()

1 个答案:

答案 0 :(得分:2)

您正在创建两组子图。一个使用

fig, axes = plt.subplots(...)

和另一个

ax = fig.add_subplot(121, projection='3d')

他们几乎做同样的事情,这就是你看到两组轴的原因。创建图形时,无需创建两个子图。而是做:

fig = plt.figure(figsize=(20, 12))

完整示例:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(7, 5))

ax = fig.add_subplot(121, projection='3d')
ax = fig.add_subplot(122, projection='3d')

plt.show()

给出:

enter image description here