Python 如何使用自定义绘图函数对matplotlib表进行子绘图?

Python 如何使用自定义绘图函数对matplotlib表进行子绘图?,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我试图创建一个函数,该函数将子地块的轴作为参数,并返回matplotlib表 我能够使用代码对条形图和线形图执行此操作。但是对一个表进行同样的尝试会给我一个“error table()为参数'ax'获取了多个值”” 我希望得到类似的结果,如下图所示 fig, ax = plt.subplots(figsize=(8,5), ncols=2) ax[1].table(cellText=df.values,rowLabels=df.index,colLabels=df.columns,loc='ri

我试图创建一个函数,该函数将子地块的轴作为参数,并返回matplotlib表

我能够使用代码对条形图和线形图执行此操作。但是对一个表进行同样的尝试会给我一个“
error table()为参数'ax'获取了多个值”

我希望得到类似的结果,如下图所示

fig, ax = plt.subplots(figsize=(8,5), ncols=2)
ax[1].table(cellText=df.values,rowLabels=df.index,colLabels=df.columns,loc='right')
# ax[1].axis('tight')
ax[1].axis('off')

我不知道我是否理解您的问题,但您可以将函数编写为:

def plot_reg(df_x, ax=None):
    ax = ax or plt.gca()  # either use the value passed as argument, or the current Axes
    g = ax.table(cellText=df_x.values,
                  rowLabels=df_x.index,
                  colLabels=df_x.columns,loc='center')
    return g
def plot_reg(df_x, ax=None):
    ax = ax or plt.gca()  # either use the value passed as argument, or the current Axes
    g = ax.table(cellText=df_x.values,
                  rowLabels=df_x.index,
                  colLabels=df_x.columns,loc='center')
    return g