在 python 中正确格式化 3d 子图
Properly format 3d subplots in python
所以我有 8 个 3d 图(7 个 3d 图和一个 2d 图)
我想以 4 x 2 格式放置它们。这是我的代码:
sensor_data = self._util_sensor(sub_df)
fig = plt.figure()
fig.tight_layout()
ax = fig.add_subplot(1, 2, 1, projection = '3d')
ax.scatter(sensor_data['chest'][0], sensor_data['chest'][1], sensor_data['chest'][2])
ax.set_title('Chest')
ax = fig.add_subplot(1, 2, 2)
ax.scatter(sensor_data['ecg'][0], sensor_data['ecg'][1])
ax.set_title('ECG')
ax = fig.add_subplot(2, 2, 1, projection = '3d')
ax.scatter(sensor_data['left_accel'][0], sensor_data['left_accel'][1], sensor_data['left_accel'][2])
ax.set_title('left accel')
ax = fig.add_subplot(2, 2, 2, projection = '3d')
ax.scatter(sensor_data['left_gyro'][0], sensor_data['left_gyro'][1], sensor_data['left_gyro'][2])
ax.set_title('left gyro')
ax = fig.add_subplot(3, 2, 1, projection = '3d')
ax.scatter(sensor_data['left_mag'][0], sensor_data['left_mag'][1], sensor_data['left_mag'][2])
ax.set_title('left mag')
ax = fig.add_subplot(3, 2, 2, projection = '3d')
ax.scatter(sensor_data['right_accel'][0], sensor_data['right_accel'][1], sensor_data['right_accel'][2])
ax.set_title('right accel')
ax = fig.add_subplot(4, 2, 1, projection = '3d')
ax.scatter(sensor_data['right_gyro'][0], sensor_data['right_gyro'][1], sensor_data['right_gyro'][2])
ax.set_title('right gyro')
ax = fig.add_subplot(4, 2, 2, projection = '3d')
ax.scatter(sensor_data['right_mag'][0], sensor_data['right_mag'][1], sensor_data['right_mag'][2])
ax.set_title('right mag')
plt.show()
结果如下图。我如何正确格式化这些?
您在 add_subplot
中使用行、列和索引的方式有问题。我希望这对您有所帮助:
fig = plt.figure()
for i in range(6):
ax = fig.add_subplot(3, 2, i+1, projection = '3d')
第一个数字是(总)行数和列数,第三个数字是“索引”,即每个子图从 1..6(=行 * 列)开始。
这是它的样子:
但要完全回答这里的问题,需要做的是:
fig = plt.figure(figsize=(6, 12))
ax1 = fig.add_subplot(3, 2, 1, projection = '3d')
ax2 = fig.add_subplot(3, 2, 2)
ax3 = fig.add_subplot(3, 2, 3, projection = '3d')
ax4 = fig.add_subplot(3, 2, 4, projection = '3d')
ax5 = fig.add_subplot(3, 2, 5, projection = '3d')
ax6 = fig.add_subplot(3, 2, 6, projection = '3d')
然后您可以使用坐标轴 (ax1..6),例如,
ax2.plot([1, 3, 2, 7])
所以我有 8 个 3d 图(7 个 3d 图和一个 2d 图)
我想以 4 x 2 格式放置它们。这是我的代码:
sensor_data = self._util_sensor(sub_df)
fig = plt.figure()
fig.tight_layout()
ax = fig.add_subplot(1, 2, 1, projection = '3d')
ax.scatter(sensor_data['chest'][0], sensor_data['chest'][1], sensor_data['chest'][2])
ax.set_title('Chest')
ax = fig.add_subplot(1, 2, 2)
ax.scatter(sensor_data['ecg'][0], sensor_data['ecg'][1])
ax.set_title('ECG')
ax = fig.add_subplot(2, 2, 1, projection = '3d')
ax.scatter(sensor_data['left_accel'][0], sensor_data['left_accel'][1], sensor_data['left_accel'][2])
ax.set_title('left accel')
ax = fig.add_subplot(2, 2, 2, projection = '3d')
ax.scatter(sensor_data['left_gyro'][0], sensor_data['left_gyro'][1], sensor_data['left_gyro'][2])
ax.set_title('left gyro')
ax = fig.add_subplot(3, 2, 1, projection = '3d')
ax.scatter(sensor_data['left_mag'][0], sensor_data['left_mag'][1], sensor_data['left_mag'][2])
ax.set_title('left mag')
ax = fig.add_subplot(3, 2, 2, projection = '3d')
ax.scatter(sensor_data['right_accel'][0], sensor_data['right_accel'][1], sensor_data['right_accel'][2])
ax.set_title('right accel')
ax = fig.add_subplot(4, 2, 1, projection = '3d')
ax.scatter(sensor_data['right_gyro'][0], sensor_data['right_gyro'][1], sensor_data['right_gyro'][2])
ax.set_title('right gyro')
ax = fig.add_subplot(4, 2, 2, projection = '3d')
ax.scatter(sensor_data['right_mag'][0], sensor_data['right_mag'][1], sensor_data['right_mag'][2])
ax.set_title('right mag')
plt.show()
结果如下图。我如何正确格式化这些?
您在 add_subplot
中使用行、列和索引的方式有问题。我希望这对您有所帮助:
fig = plt.figure()
for i in range(6):
ax = fig.add_subplot(3, 2, i+1, projection = '3d')
第一个数字是(总)行数和列数,第三个数字是“索引”,即每个子图从 1..6(=行 * 列)开始。
这是它的样子:
但要完全回答这里的问题,需要做的是:
fig = plt.figure(figsize=(6, 12))
ax1 = fig.add_subplot(3, 2, 1, projection = '3d')
ax2 = fig.add_subplot(3, 2, 2)
ax3 = fig.add_subplot(3, 2, 3, projection = '3d')
ax4 = fig.add_subplot(3, 2, 4, projection = '3d')
ax5 = fig.add_subplot(3, 2, 5, projection = '3d')
ax6 = fig.add_subplot(3, 2, 6, projection = '3d')
然后您可以使用坐标轴 (ax1..6),例如,
ax2.plot([1, 3, 2, 7])