Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ License: GNU General Public License v3.0
options:
-h, --help show this help message and exit
-b, --bitrate BITRATE
bitrate (in kbps) of the encoded video (default: 1800)
bitrate (in kbps) of the encoded MP4 video, ignored
for GIF output (default: 1800)
-f, --outfile OUTFILE
name of the output MP4 file; if not set the animation
is displayed interactively instead
name of the output file (.mp4 or .gif); if not set the
animation is displayed interactively instead
--fps FPS frames per second of the encoded video (default: 25)
--hold HOLD seconds to hold the last frame at the end of the
animation (default: 2.0)
Expand All @@ -257,17 +258,19 @@ examples:
hadcrut5_animate.py --global
hadcrut5_animate.py --period "1880-1920"
hadcrut5_animate.py --period "1880-1920" --outfile HadCRUT5-1880-1920-animation.mp4
hadcrut5_animate.py --period "1880-1920" --outfile HadCRUT5-1880-1920-animation.gif
hadcrut5_animate.py --fps 30 --hold 5 --outfile HadCRUT5-animation.mp4
```

This script requires `ffmpeg` to be installed, and reveals the selected regions year by year instead of drawing the
whole time series at once. Without `--outfile` the animation is displayed interactively; with it, the animation is
encoded to an MP4 file.
This script reveals the selected regions year by year instead of drawing the whole time series at once. Without
`--outfile` the animation is displayed interactively. An `.mp4` output requires `ffmpeg` to be installed; a `.gif`
output only needs Pillow (already a Matplotlib dependency). GitHub's Markdown renderer strips `<video>` tags for
repo-hosted files, so the GIF format is what's embedded below.

```
$ ./hadcrut5_animate.py --period "1880-1920" --outfile plots/HadCRUT5-1880-1920-animation.mp4
$ ./hadcrut5_animate.py --period "1880-1920" --outfile plots/HadCRUT5-1880-1920-animation.gif
```
<video src="plots/HadCRUT5-1880-1920-animation.mp4" controls loop autoplay muted playsinline></video>
![HadCRUT5 animated plot of the temperature anomalies related to 1880-1920](plots/HadCRUT5-1880-1920-animation.gif)

# License

Expand Down
17 changes: 13 additions & 4 deletions hadcrut5_animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def parse_args() -> argparse.Namespace:
"%(prog)s --global",
'%(prog)s --period "1880-1920"',
'%(prog)s --period "1880-1920" --outfile HadCRUT5-1880-1920-animation.mp4',
'%(prog)s --period "1880-1920" --outfile HadCRUT5-1880-1920-animation.gif',
"%(prog)s --fps 30 --hold 5 --outfile HadCRUT5-animation.mp4",
]

Expand All @@ -63,15 +64,16 @@ def parse_args() -> argparse.Namespace:
dest="bitrate",
default=1800,
type=int,
help="bitrate (in kbps) of the encoded video (default: 1800)",
help="bitrate (in kbps) of the encoded MP4 video, ignored for GIF "
"output (default: 1800)",
)
parser.add_argument(
"-f",
"--outfile",
action="store",
dest="outfile",
help="name of the output MP4 file; if not set the animation is "
"displayed interactively instead",
help="name of the output file (.mp4 or .gif); if not set the "
"animation is displayed interactively instead",
)
parser.add_argument(
"--fps",
Expand Down Expand Up @@ -237,7 +239,14 @@ def update(frame):
)

if outfile:
writer = animation.FFMpegWriter(fps=fps, bitrate=bitrate)
writer: animation.AbstractMovieWriter
if outfile.lower().endswith(".gif"):
# GitHub's Markdown renderer strips <video> tags for repo-hosted
# files, so a GIF (embeddable as a normal image) is required to
# show the animation inline in a README.
writer = animation.PillowWriter(fps=fps)
else:
writer = animation.FFMpegWriter(fps=fps, bitrate=bitrate)
ani.save(outfile, writer=writer)
else:
plt.show()
Expand Down
2 changes: 1 addition & 1 deletion plots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ echo "creating plot closing in to 1.5°C ..."
echo "creating animated plot ..."
./hadcrut5_animate.py \
--period "1880-1920" \
--outfile plots/HadCRUT5-1880-1920-animation.mp4
--outfile plots/HadCRUT5-1880-1920-animation.gif

echo "done."
Binary file added plots/HadCRUT5-1880-1920-animation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed plots/HadCRUT5-1880-1920-animation.mp4
Binary file not shown.
Loading