Matplotlib Savefig函数在“透明”为真时在自身上绘制轴

Matplotlib Savefig函数在“透明”为真时在自身上绘制轴,matplotlib,transparency,Matplotlib,Transparency,我有一个程序,可以处理数据并以多轴图形的形式显示结果。我有许多不同的图形集,我正试图将它们生成为报告格式。为了节省内存,我创建了一个figure实例,并在每个循环结束时清除。以下是表格示例: import matplotlib.pyplot as plt import numpy as np def the_figure(): #I want a figure that is persistent and accessible #So I make the figure an

我有一个程序,可以处理数据并以多轴图形的形式显示结果。我有许多不同的图形集,我正试图将它们生成为报告格式。为了节省内存,我创建了一个figure实例,并在每个循环结束时清除。以下是表格示例:

import matplotlib.pyplot as plt
import numpy as np

def the_figure():
    #I want a figure that is persistent and accessible
    #So I make the figure an attribute of a function
    the_figure.fig = plt.figure()
    the_figure.axes = dict(
                    t_ax = plt.subplot2grid((6,2),(1,0)),
                    t_fit_ax = plt.subplot2grid((6,2),(1,1)),
                    o_ax = plt.subplot2grid((6,2),(2,0)),
                    o_fit_ax = plt.subplot2grid((6,2),(2,1)),
                    table = plt.subplot2grid((6,2),(3,0), 
                                    rowspan = 3, colspan = 2)
                    )

#A function which makes figures using the single figure function       
def Disp(i=5):
    try:
        the_figure.fig
    except:
        the_figure()

    pi = 3.141592653589793
    axes = the_figure.axes
    xs = np.linspace(-pi/2,pi/2)

    for n in range(i):
        for name,ax in axes.items():
            ax.plot(xs,np.sin(xs*n))

        the_figure.fig.savefig('test_folder\\bad'+str(n),transparent=True)
        the_figure.fig.savefig('test_folder\\good'+str(n),transparent=False)

        #Clear the axes for reuse, supposedly 
        for name,ax in axes.items():
            ax.cla()
完成后,图形以transparent=True保存,以获得来自循环的曲线和来自上一个循环的曲线的叠加。我不知道发生了什么事


要清除轴,您正在使用
ax.cla()
,但是您需要发出
ax.clear()
(或者可能是
plt.axs(ax.cla()
)。

要清除轴,您正在使用
ax.cla()
,但是您需要发出
ax.clear()
(或者可能是
plt.ax.cla()
)。

好的,我想我明白了。cla()适用于当前axis对象,而.clear()适用于它所属的axis实例。我刚刚将ax.cla()更改为ax.clear,得到了相同的行为。我还尝试更改plt.axes(ax).cla(),得到了相同的行为。不过,更改后端可以解决问题。”WXAgg'显示行为,而使用'Qt4Agg'作为后端修复问题。使用Qt4Agg作为后端,使用.cla()、.clear()或plt.axes(ax.cla()清除轴并不重要。因此,我找到了一个“解决方案”,但我不明白它为什么起作用,或者问题是什么。基于后端的差异,您可能会在github上matplotlib的问题跟踪程序中提交一个错误。好的,我想我明白了。cla()适用于当前axis对象,而.clear()适用于它所属的axis实例。我刚刚将ax.cla()更改为ax.clear,得到了相同的行为。我还尝试更改plt.axes(ax).cla(),得到了相同的行为。不过,更改后端可以解决问题。”WXAgg'显示行为,而使用'Qt4Agg'作为后端修复问题。使用Qt4Agg作为后端,使用.cla()、.clear()或plt.axes(ax.cla()清除轴并不重要。因此,我找到了一个“解决方案”,但我不明白它为什么起作用,也不明白问题是什么。基于后端的差异,您可能会在github上matplotlib的问题跟踪程序中提交一个bug。
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(1) # This is as persistent as assigning to whatever function
def init_axes(fig):
   fig.clear()
   return dict(
                   t_ax = plt.subplot2grid((6,2),(1,0)),
                   t_fit_ax = plt.subplot2grid((6,2),(1,1)),
                   o_ax = plt.subplot2grid((6,2),(2,0)),
                   o_fit_ax = plt.subplot2grid((6,2),(2,1)),
                   table = plt.subplot2grid((6,2),(3,0), 
                                   rowspan = 3, colspan = 2)
                   )
#A function which makes figures using the single figure       
def Disp(i=5):

   pi = 3.141592653589793
   xs = np.linspace(-pi/2,pi/2)

   for n in range(i):
       axes = init_axes(fig)
       for name,ax in axes.items():
           ax.plot(xs,np.sin(xs*n))

       fig.savefig('bad'+str(n),transparent=True)
       fig.savefig('good'+str(n),transparent=False)