具有透明背景的 mplot3d 动画 [英] mplot3d animation with transparent background

查看:59
本文介绍了具有透明背景的 mplot3d 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用matplotlib生成一些透明背景的gif.我尝试了不同的选项,但无法获取具有透明背景的文件.在当前设置下,我得到了这样的第一帧,而其余部分则没有.以下是我的代码

I want to generate some gifs with transparent background using matplotlib. I tried different options but I can't get my files with transparent background. With the current setup I get the first frame like that but not the rest. The following is my code

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

结果就是这样(如果单击它,您会看到只有一帧是透明的)

That gives as a result (if you click on it, you can see that just one frame is transparent)

问题:是否可以使用mplot3d获得具有透明背景的动画?

Question: Is there a way to get the animation with transparent background using mplot3d?

推荐答案

我认为这确实是一个错误.但是,如果您更关心结果而不是到达结果的方式,则可以使用以下方法.您可以分别保存每个图像,然后调用imageMagick将它们转换为动画gif,而不是调用动画.请参阅下面的代码并注意 convert.exe 的参数.

I think it's really a bug. However, if you rather care about the result than the way to get there, the following would do the job. Instead of calling animation, you can save each image separately and then call imageMagick to convert them to an animated gif. See the code below and mind the arguments to 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)进行了测试.

Mind that the calls to imageMagick and the delete command might be system dependend. This script has been tested with imageMagick 7.0.3 (Q16) under Windows 8.

这篇关于具有透明背景的 mplot3d 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆