首页 > 解决方案 > 一次处理多个绘图对象

问题描述

我有一个带有循环的方法,它创建由给定数据框的列表输入给出的多个箱线图:

def create_boxplots(data_frame):
    x_axis_attr = [columnA, columnB, columnC]
    for index in range(len(x_axis_attr)):
         boxplot = sns.boxplot(x=data_frame[x_axis_attr[index]])
         plot_dict.update({"boxplot " + x_axis_attr[index]: boxplot})

    return plot_dict

绘图保存由不同的模块处理,因此创建的绘图在 dict() 中返回。

事实证明,这些图没有保存在不同的对象中:

[<matplotlib.axes._subplots.AxesSubplot object at 0x181640D0>, 
<matplotlib.axes._subplots.AxesSubplot object at 0x181640D0>, 
<matplotlib.axes._subplots.AxesSubplot object at 0x181640D0>]

即使我用以下方式保存数字boxplot.get_figure()

[<matplotlib.figure.Figure object at 0x18C37F30>, 
 <matplotlib.figure.Figure object at 0x18C37F30>, 
 <matplotlib.figure.Figure object at 0x18C37F30>]

注意:1.如果plt.show()在循环中执行,所有箱线图都正确显示

如何确保所有地块都有自己的对象并且共享内存?提前致谢

标签: pythondataframematplotlibplotseaborn

解决方案


推荐阅读