mplot3d动画与透明背景

时间:2016-07-16 00:33:43

标签: python matplotlib animated-gif mplot3d

我想使用matplotlib生成一些透明背景的GIF。我尝试了不同的选项,但我无法获得透明背景的文件。使用当前设置,我得到第一帧,但不是其余的。以下是我的代码

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

1 个答案:

答案 0 :(得分: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)进行了测试。