2016-07-16 152 views
8

我想使用matplotlib生成一些透明背景的gif。我嘗試了不同的選項,但無法使用透明背景獲取我的文件。在目前的設置下,我得到了這樣的第一幀,但沒有其他的。下面是我的代碼帶透明背景的mplot3d動畫

from __future__ import division 
from numpy import pi, sin, cos, mgrid 
from scipy.special import jn, jn_zeros 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import rcParams 


# In Windows the next line should provide the full path to convert.exe 
# since convert is a Windows command 
rcParams['animation.convert_path'] = "C:\Program Files\ImageMagick-6.9.3\convert.exe" 
rcParams['savefig.transparent'] = True 
rcParams['savefig.dpi'] = 130 
rcParams['savefig.pad_inches'] = 0 
plot_args = {'rstride': 1, 'cstride': 1, 'cmap':"RdYlBu", 
      'linewidth': 0.5, 'antialiased': True, 'color': '#1e1e1e', 
      'shade': True, 'alpha': 1.0, 'vmin': -1, 'vmax':1} 


def data_gen(num): 
    ax.cla() 
    m, n = 1, 2 
    lam = jn_zeros(m, n)[-1] 
    dt = 2*pi/(30*lam) 
    z = cos(m*t)*jn(m, lam*r)*sin(lam*num*dt) 
    surf = ax.plot_surface(x, y, z, **plot_args) 
    ax.view_init(elev=30, azim=45) 
    ax.set_xlim(-0.6, 0.6) 
    ax.set_ylim(-0.6, 0.6) 
    ax.set_zlim(-1, 1) 
    plt.axis("off") 
    return surf 


r, t = mgrid[0:1:20j, 0:2*pi:40j] 
x, y = r*cos(t), r*sin(t) 
fig = plt.figure(facecolor=None) 
ax = fig.add_subplot(111, projection='3d') 
ani = animation.FuncAnimation(fig, data_gen, range(30), blit=False) 
ani.save("Drum vibration mode.gif", writer='imagemagick') 

這給了結果(如果你點擊它,你可以看到,只是一個框架是透明的)

問:有沒有辦法讓動畫透明背景使用mplot3d?

enter image description here

+0

您是否看到這個http://blog.adrian.pw/posts/2016/06/matplotlib-transparent-animation/?看來你正在尋找的是'fig.patch.set_alpha(0)'和類似'ani.save('circle_anim.mov',codec =「png」,dpi = 100,bitrate = -1, savefig_kwargs = {'transparent':True,'facecolor':'none'})'。我現在不能嘗試,但稍後會放棄。 –

+0

@JacquesGaudin,我剛剛檢查過它。這很酷,但它不會將圖像導出爲GIF。事實上,使用GIF編寫器時會忽略''savefig_kwargs''。 – nicoguaro

+0

@JacquesGaudin,剛剛嘗試:不起作用。 – nicoguaro

回答

4

我認爲這是一個真正的錯誤。 但是,如果您更關心的是結果而不是實現目標的方式,那麼下面的工作就可以完成。您可以單獨保存每個圖像,然後調用imageMagick將其轉換爲動畫gif,而不是調用動畫。 請參閱下面的代碼並介紹convert.exe的參數。

from __future__ import division 
from numpy import pi, sin, cos, mgrid 
from scipy.special import jn, jn_zeros 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 

from matplotlib import rcParams 
import subprocess 


# In Windows the next line should provide the full path to convert.exe 
# since convert is a Windows command 
path_to_convert = "C:\Program Files\ImageMagick-6.9.3\convert.exe" 
#rcParams['animation.convert_path'] = path_to_convert 
rcParams['savefig.transparent'] = True 
rcParams['savefig.dpi'] = 130 
rcParams['savefig.pad_inches'] = 0 
plot_args = {'rstride': 1, 'cstride': 1, 'cmap':"RdYlBu", 
      'linewidth': 0.5, 'antialiased': True, 'color': '#1e1e1e', 
      'shade': True, 'alpha': 1.0, 'vmin': -1, 'vmax':1} 


def data_gen(num): 
    ax.cla() 
    ax.clear() 
    m, n = 1, 2 
    lam = jn_zeros(m, n)[-1] 
    dt = 2*pi/(30*lam) 
    z = cos(m*t)*jn(m, lam*r)*sin(lam*num*dt) 
    surf = ax.plot_surface(x, y, z, **plot_args) 
    ax.view_init(elev=30, azim=45) 
    ax.set_xlim(-0.6, 0.6) 
    ax.set_ylim(-0.6, 0.6) 
    ax.set_zlim(-1, 1) 
    ax.axis("off") 
    fig.patch.set_visible(False) 
    ax.patch.set_visible(False) 
    ax.set_axis_off() 
    ax._axis3don = False 
    return surf 


r, t = mgrid[0:1:20j, 0:2*pi:40j] 
x, y = r*cos(t), r*sin(t) 
fig = plt.figure(facecolor=None, frameon=False) 
ax = fig.add_subplot(111, projection='3d') 
for i in range(30): 
    data_gen(i) 
    plt.savefig("drum_{n:02d}.png".format(n=i), transparent=True, frameon=False) 
    print i, 

args = [path_to_convert, "-delay", "10", "-loop" , "0", "-dispose", "Background", "drum_*.png", "output.gif"] 
subprocess.call(args, shell=True) 
subprocess.call(["del", "/Q", "drum_*.png"], shell=True) 
print "\ndone" 

請注意,調用imageMagick和delete命令可能是系統依賴。該腳本已經在Windows 8下使用imageMagick 7.0.3(Q16)進行了測試。

+0

我正在考慮這個選項,但是希望有人能以其他方式做到這一點。爲了讓你的答案變得有效,我將''args''行改爲 ''args = [path_to_convert,「drum_ * .png」,「-delay」,「10」,「-loop」,「0」,「處理「,」背景「,」output.gif「]''。 – nicoguaro

+0

如果我們可以添加諸如subprocess.call([「rm」,「drum _ *。png」,shell = False)''來擦除PNG文件並保留GIF,那將會很棒。 – nicoguaro

+0

我添加了一行來擺脫png文件。 – ImportanceOfBeingErnest