python动态柱状图_celluloid:用于创建拥有动画效果的Matplotlib图表

Celluloid是一个简洁的库,旨在简化matplotlib中创建动画的过程。它允许你将现有的可视化代码适配到动画中。只需创建一个figure,然后通过Camera类捕获每一帧,最后通过animate方法生成动画并保存。例子包括简单的线性动画、子图动画、图像动画和带有图例的动画。但请注意,该库存在一些限制,如轴限制和图例处理。
摘要由CSDN通过智能技术生成

celluloid

68747470733a2f2f7472617669732d63692e636f6d2f6a776b76616d2f63656c6c756c6f69642e7376673f6272616e63683d6d617374657268747470733a2f2f636f6465636f762e696f2f67682f6a776b76616d2f63656c6c756c6f69642f6272616e63682f6d61737465722f67726170682f62616467652e73766768747470733a2f2f62616467652e667572792e696f2f70792f63656c6c756c6f69642e73766768747470733a2f2f696d672e736869656c64732e696f2f707970692f707976657273696f6e732f63656c6c756c6f69642e737667

Easy Matplotlib Animation

48657442-9c11e080-e9e5-11e8-9f54-f46a960be7dd.gif

Creating animations should be easy. This module makes it easy to adapt your existing visualization code to create an animation.

Install

pip install celluloid

Manual

Follow these steps:

Create a matplotlib Figure and create a Camera from it:

from celluloid import Camera

fig = plt.figure()

camera = Camera(fig)

Reusing the figure and after each frame is created, take a snapshot with the camera.

plt.plot(...)

plt.fancy_stuff()

camera.snap()

After all frames have been captured, create the animation.

animation = camera.animate()

animation.save('animation.mp4')

The entire module is less than 50 lines of code.

Examples

Minimal

As simple as it gets.

from matplotlib import pyplot as plt

from celluloid import Camera

fig = plt.figure()

camera = Camera(fig)

for i in range(10):

plt.plot([i] * 10)

camera.snap()

animation = camera.animate()

48666133-66660980-ea70-11e8-9024-b167c21a5e83.gif

Subplots

Animation at the top.

import numpy as np

from matplotlib import pyplot as plt

from celluloid import Camera

fig, axes = plt.subplots(2)

camera = Camera(fig)

t = np.linspace(0, 2 * np.pi, 128, endpoint=False)

for i in t:

axes[0].plot(t, np.sin(t + i), color='blue')

axes[1].plot(t, np.sin(t - i), color='blue')

camera.snap()

animation = camera.animate()

Images

Domain coloring example.

import numpy as np

from matplotlib import pyplot as plt

from matplotlib.colors import hsv_to_rgb

from celluloid import Camera

fig = plt.figure()

camera = Camera(fig)

for a in np.linspace(0, 2 * np.pi, 30, endpoint=False):

x = np.linspace(-3, 3, 800)

X, Y = np.meshgrid(x, x)

x = X + 1j * Y

y = (x ** 2 - 2.5) * (x - 2.5 * 1j) * (x + 2.5 * 1j) \

* (x - 2 - 1j) ** 2 / ((x - np.exp(1j * a)) ** 2

* (x - np.exp(1j * 2 * a)) ** 2)

H = np.angle(y) / (2 * np.pi) + .5

r = np.log2(1. + np.abs(y))

S = (1. + np.abs(np.sin(2. * np.pi * r))) / 2.

V = (1. + np.abs(np.cos(2. * np.pi * r))) / 2.

rgb = hsv_to_rgb(np.dstack((H, S, V)))

ax.imshow(rgb)

camera.snap()

animation = camera.animate()

48747098-f483f080-ec26-11e8-9734-c409e9b0c9ec.gif

Legends

import matplotlib

from matplotlib import pyplot as plt

from celluloid import Camera

fig = plt.figure()

camera = Camera(fig)

for i in range(5):

t = plt.plot(range(i, i + 5))

plt.legend(t, [f'line{i}'])

camera.snap()

animation = camera.animate()

48750564-9100bf80-ec34-11e8-87fb-bc5c7ddcc6e7.gif

Limitations

The axes' limits should be the same for all plots. The limits of the animation will be the limits of the final plot.

Legends will accumulate from previous frames. Pass the artists to the legend function to draw them separately.

This can demand a lot of memory since it uses ArtistAnimation under the hood. This means that all artists are saved to memory before the animation is constructed.

This is a black box. If you want to understand how matplotlib animations work, using this library may hinder you. If you want to be an expert matplotlib user, you may want to pass on this library.

Credits

Inspired by plotnine.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值