如果透明为真,Matplotlib Savefig函数在自身上绘制轴。

问题描述 投票:0回答:2

我有一个程序,可以将数据压缩并将结果显示在多轴图中。我有许多不同的数字集,我想把它们生成一个报告格式。为了节省内存,我正在制作一个单一的数字实例,并在每个循环结束时清除。下面是一个表格的例子。

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

当它完成时,保存的数字,如果透明=True,就会得到他们循环中的曲线和上一个循环中的曲线的叠加。我不知道这是怎么回事。

With Transparency

Without Transparency

matplotlib transparency
2个回答
0
投票
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)

0
投票

要清除你的轴,你正在使用 ax.cla()但你需要发出 ax.clear() (或可能 plt.axes(ax).cla()).

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.