Similar to ``xmovie``, make animations from xarray objects.
Project description
xrframes
Similar to (and inspired by) jbusecke/xmovie, make animations from xarray objects by applying your plotting function along a dimension.
Install
pip install xrframes
Install and use Dask[^a] or joblib[^b] to parallelize the frame creation step.
.to_gif() uses ImageMagick (magick or convert required on PATH);
.to_mp4() uses FFmpeg (ffmpeg required on PATH).
Both are available via conda-forge[^c], as well as other package managers.
Example
Basic example, using just the xarray object's plot method:
import xarray as xr
from xrframes import Frames
# Note: `pooch` required
ta = xr.tutorial.open_dataset("air_temperature").air.isel(time=slice(0, 10))
frames = Frames(ta, lambda da: da.plot(size=2.5, aspect=1.7), dim="time")
frames.write(dpi=120) # serial
frames.to_gif("./ta_basic.gif", fps=5)
A bit fancier example with the same data, using Cartopy:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import xarray as xr
from xrframes import Frames
# Note: `pooch` required
ta = xr.tutorial.open_dataset("air_temperature").air.isel(time=slice(0, 10))
proj = ccrs.Mercator()
tran = ccrs.PlateCarree()
def plot(da, **kwargs):
fig, ax = plt.subplots(figsize=(6, 3), layout="constrained", subplot_kw=dict(projection=proj))
ax.coastlines()
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.STATES)
ax.gridlines(draw_labels=True)
da.plot.contourf(**kwargs, levels=np.arange(230, 305, 5), extend="both", ax=ax, transform=tran)
ax.set_title("")
ax.set_title(pd.Timestamp(da.time.item()).strftime(r"%Y-%m-%d %H:%M"), loc="left", size=10)
return fig
frames = Frames(ta, plot, dim="time")
frames.write() # serial
frames.to_mp4("./ta_cartopy.mp4", fps=5)
[^a]: dask on PyPI, dask-core on conda-forge, more info here
[^b]: joblib on PyPI and on conda-forge
[^c]: conda install -c conda-forge imagemagick ffmpeg
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xrframes-0.0.3.tar.gz.
File metadata
- Download URL: xrframes-0.0.3.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26f8ce566dec4d95dcbe9ec5e574522e16336cd27bf41bd48ce11b1b90c4e5d0
|
|
| MD5 |
1ebaa3bc02afabe833ccfd7476ca930a
|
|
| BLAKE2b-256 |
feab8b3b839f36c7e60b8bcbe0d008ef8871cae21f17428377abde5ca5148477
|
File details
Details for the file xrframes-0.0.3-py3-none-any.whl.
File metadata
- Download URL: xrframes-0.0.3-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afa1479d8b4e77e4a2c4dbb61d36a276daa6339a564c7a1fbd89df65abde7637
|
|
| MD5 |
89cd038130935698dc0e15b941e1e662
|
|
| BLAKE2b-256 |
a8659150285b72018381267b6bce44aa0ee80051406d6eddde3f1e9538c86def
|