matplotlib 如何将合并3D投影与2D子图结合并设置宽度

x7yiwoj4  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(119)

我有工作代码来生成图表,显示来自加速度计的三个参数的x,y,z值,每个参数都有并排的线和3D图:

from mpl_toolkits import mplot3d
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#code here loads data into a dataframe df

fig = plt.figure(figsize=(10,8))
fig.suptitle(filename, fontsize=12)
for p in ('accel','angle','avelo')
    i += 1
    ax = fig.add_subplot(3, 2, i)
    ax.plot(idx,df[p,'x'], label = "x")
    ax.plot(idx,df[p,'y'], label = "y")
    ax.plot(idx,df[p,'z'], label = "z")
    ax.set_ylabel(p)
    ax.legend(loc="best")
    i += 1
    ax = fig.add_subplot(3,2,i,projection='3d')
    ax.plot3D(df[p,'x'],df[p,'y'],df[p,'z'],'black')
    ax.scatter(df[p]['x'][0],df[p]['y'][0],df[p]['z'][0], c='green', marker='o', s=50)
    ax.scatter(df[p]['x'].iloc[-1],df[p]['y'].iloc[-1],df[p]['z'].iloc[-1], c='red', marker='x', s=50)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')    
plt.subplots_adjust(left=0.1,
                    bottom=0.1,
                    right=0.9,
                    top=0.9,
                    wspace=0.4,
                    hspace=0.1)
plt.show()

我想让线条图的宽度是默认值的两倍。有没有什么方法可以用现有的add_subplot方法来实现这一点,或者我必须重新编写代码来使用plt.subplots来设置图?我找到的所有例子都假设后者。

m528fe3b

m528fe3b1#

import matplotlib.pyplot as plt

# create the figure and axes with specified width_ratios
fig, axes = plt.subplots(3, 2, figsize=(10, 10), width_ratios=[2, 1])

# remove the subplots to be set as 3d projections
axes[0, 1].remove()
axes[1, 1].remove()
axes[2, 1].remove()

# add the subplots back as 3d projections; rows, cols and index are relative to width_ratios
axes[0, 1] = fig.add_subplot(3, 3, 3, projection='3d')
axes[1, 1] = fig.add_subplot(3, 3, 6, projection='3d')
axes[2, 1] = fig.add_subplot(3, 3, 9, projection='3d')

cols = ['accel','angle','avelo']

# axes is a (3, 2) array; iterate through each set of subplots, and corresponding value from cols
for (ax_left, ax_right), col in zip(axes, cols):
    
#     ax_left.plot(..., label='x')
#     ax_left.plot(..., label='y')
#     ax_left.plot(..., label='z')
    ax_left.set_ylabel(col)
    
#     ax_right.plot3d(...)
#     ax_right.scatter(...)
#     ax_right.scatter(...)

    # move the z-axis to the left side, otherwise the label isn't visible
    ax_right.zaxis._axinfo['juggled'] = (1, 2, 2)

    ax_right.set_xlabel('x')
    ax_right.set_ylabel('y')
    ax_right.set_zlabel('z')

# create subplot mosaic with different keyword arguments
fig, ax = plt.subplot_mosaic("AB;CD;EF",
                             per_subplot_kw={('B', 'D', 'F'): {'projection': '3d'}},
                             gridspec_kw={'width_ratios': [2, 1],
                                          'wspace': 0.1, 'hspace': 0.1},
                             figsize=(10, 10))

相关问题