【问题标题】:Properly format 3d subplots正确格式化 3d 子图
【发布时间】:2022-10-09 06:58:13
【问题描述】:

所以我有 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()

结果如下图。如何正确格式化这些?

【问题讨论】:

    标签: python matplotlib plot 3d


    【解决方案1】:

    add_subplot 中使用行、列和索引的方式有问题。我希望这会有所帮助:

    fig = plt.figure()
    for i in range(6):
        ax = fig.add_subplot(3, 2, i+1, projection = '3d')
    

    第一个数字是(总)行数和列数,第三个数字是“索引”,即每个子图从 1..6(=rows * columns)开始。

    这是它的样子:

    但是要完全回答这里的问题,需要做的是:

    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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2013-09-18
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多