Created
July 30, 2018 07:52
-
-
Save simecek/362ae1403cb64bf09158c1ddd4e95780 to your computer and use it in GitHub Desktop.
GIF_imageio.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "GIF_imageio.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"[View in Colaboratory](https://colab.research.google.com/gist/simecek/362ae1403cb64bf09158c1ddd4e95780/gif_imageio.ipynb)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "IKI9ixcuGrK5", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## How to create an animated GIF with imageio\n", | |
"\n", | |
"This is an example how to create an animated GIF with [imageio](https://imageio.github.io/), Python library for reading and writing image data. It was inspired by Thomas Pedersen's [gganimate](https://github.com/thomasp85/gganimate) R package and [Yan Holtz's animation](https://python-graph-gallery.com/341-python-gapminder-animation/) from The Python graph gallery. Data are subset of the [Gapminder World](https://www.gapminder.org/data/) dataset." | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "14R2KbRbFrvB", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Install plotnine and imageio" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "fq7s9s7JpSrv", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 391 | |
}, | |
"outputId": "694bf9bc-3d8a-4b51-b459-5fd8bd7ee8a8" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!pip install plotnine\n", | |
"!pip install imageio" | |
], | |
"execution_count": 81, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: plotnine in /usr/local/lib/python3.6/dist-packages (0.3.0)\r\n", | |
"Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from plotnine) (1.11.0)\r\n", | |
"Requirement already satisfied: pandas>=0.21.0 in /usr/local/lib/python3.6/dist-packages (from plotnine) (0.22.0)\r\n", | |
"Requirement already satisfied: statsmodels>=0.8.0 in /usr/local/lib/python3.6/dist-packages (from plotnine) (0.8.0)\r\n", | |
"Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from plotnine) (1.14.5)\r\n", | |
"Requirement already satisfied: mizani>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from plotnine) (0.4.6)\n", | |
"Requirement already satisfied: patsy>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from plotnine) (0.5.0)\n", | |
"Requirement already satisfied: matplotlib>=2.1.0 in /usr/local/lib/python3.6/dist-packages (from plotnine) (2.1.2)\n", | |
"Requirement already satisfied: scipy>=1.0.0 in /usr/local/lib/python3.6/dist-packages (from plotnine) (1.1.0)\n", | |
"Requirement already satisfied: pytz>=2011k in /usr/local/lib/python3.6/dist-packages (from pandas>=0.21.0->plotnine) (2018.5)\n", | |
"Requirement already satisfied: python-dateutil>=2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.21.0->plotnine) (2.5.3)\n", | |
"Requirement already satisfied: palettable in /usr/local/lib/python3.6/dist-packages (from mizani>=0.4.1->plotnine) (3.1.1)\n", | |
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.1.0->plotnine) (2.2.0)\n", | |
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.1.0->plotnine) (0.10.0)\n", | |
"Collecting imageio\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/a7/1d/33c8686072148b3b0fcc12a2e0857dd8316b8ae20a0fa66c8d6a6d01c05c/imageio-2.3.0-py2.py3-none-any.whl (3.3MB)\n", | |
"\u001b[K 100% |████████████████████████████████| 3.3MB 6.7MB/s \n", | |
"\u001b[?25hRequirement already satisfied: pillow in /usr/local/lib/python3.6/dist-packages (from imageio) (4.0.0)\n", | |
"Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from imageio) (1.14.5)\n", | |
"Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from pillow->imageio) (0.45.1)\n", | |
"Installing collected packages: imageio\n", | |
"Successfully installed imageio-2.3.0\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "3-tkcbpeF1AV", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Load libraries" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "vwxMT7TAYAec", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import pandas as pd\n", | |
"import imageio\n", | |
"from plotnine import * # plotting\n", | |
"from google.colab import files # file download" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "wzmXe0jJGB33", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Get the gapminder data" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "JZ50aSB4YNjY", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "1e3b2464-957e-4fce-b61d-1381f9c55d5c" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"url = 'https://raw.githubusercontent.com/jennybc/gapminder/master/inst/extdata/gapminder.tsv'\n", | |
"data = pd.read_table(url, sep=\"\\t\")\n", | |
"data.shape" | |
], | |
"execution_count": 74, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(1704, 6)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 74 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "AfsSC0V0GK8q", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Create a plot for each year" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "XyIYXSUdYX09", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 493 | |
}, | |
"outputId": "724ddbdf-fe9a-4749-e043-ce69c72ff852" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"plots = list()\n", | |
"years = sorted(data.year.unique())\n", | |
"for y in years:\n", | |
" tmp = data[ data.year == y ]\n", | |
" (ggplot(tmp, aes(x='gdpPercap', y='lifeExp', fill='continent')) +\n", | |
" geom_point(size = np.sqrt(data['pop'])/np.sqrt(1000000), alpha = 0.7) +\n", | |
" scale_x_log10(limits = [200, 60_000]) +\n", | |
" ylim(24, 87) +\n", | |
" ggtitle(\"Year: \" + str(y)) +\n", | |
" xlab('GDP per capita') +\n", | |
" ylab('life expectancy') +\n", | |
" theme_bw()\n", | |
" ).save(\"year\" + str(y) + \".png\") " | |
], | |
"execution_count": 92, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:688: UserWarning: Saving 6.4 x 4.8 in image.\n", | |
" from_inches(height, units), units))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1952.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/layer.py:450: UserWarning: geom_point : Removed 1 rows containing missing values.\n", | |
" self.data = self.geom.handle_na(self.data)\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1957.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1962.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1967.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1972.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1977.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1982.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1987.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1992.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year1997.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year2002.png\n", | |
" warn('Filename: {}'.format(filename))\n", | |
"/usr/local/lib/python3.6/dist-packages/plotnine/ggplot.py:689: UserWarning: Filename: year2007.png\n", | |
" warn('Filename: {}'.format(filename))\n" | |
], | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "X2lBP-kWGUpZ", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Save the animated GIF" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "Vfm-RNdDdAGB", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"images = list()\n", | |
"for y in years:\n", | |
" images.append(imageio.imread('year' + str(y) + '.png'))\n", | |
"imageio.mimsave(\"gapminer.gif\", images)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "xorwqYcYGmhg", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"### Download the GIF from Colab to your computer" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "0lH_IhHIEhCH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"files.download('gapminer.gif')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twitter.com/python_tip/status/1023842213116035073