matplotlib 如何更改使用add_subplot添加的子图的大小

7nbnzgx9  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(150)

我有工作代码来生成显示加速度计三个参数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.subplot设置这些图?我找到的所有例子都假设后者。

xuo3flqw

xuo3flqw1#

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

相关问题