Skip to content

Instantly share code, notes, and snippets.

@JiaweiZhuang
Created April 3, 2018 03:00
Show Gist options
  • Save JiaweiZhuang/bf192ae383bb93da1c611476504c64e7 to your computer and use it in GitHub Desktop.
Save JiaweiZhuang/bf192ae383bb93da1c611476504c64e7 to your computer and use it in GitHub Desktop.
xarray interactive plot
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import cartopy.crs as ccrs\n",
"import numpy as np\n",
"import xarray as xr\n",
"\n",
"from ipywidgets import interact, IntSlider"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (lat: 25, lon: 53, time: 2920)\n",
"Coordinates:\n",
" * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 62.5 60.0 57.5 55.0 52.5 ...\n",
" * lon (lon) float32 200.0 202.5 205.0 207.5 210.0 212.5 215.0 217.5 ...\n",
" * time (time) datetime64[ns] 2013-01-01 2013-01-01T06:00:00 ...\n",
"Data variables:\n",
" air (time, lat, lon) float32 241.2 242.5 243.5 244.0 244.1 243.89 ...\n",
"Attributes:\n",
" Conventions: COARDS\n",
" title: 4x daily NMC reanalysis (1948)\n",
" description: Data is from NMC initialized reanalysis\\n(4x/day). These a...\n",
" platform: Model\n",
" references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly..."
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds = xr.tutorial.load_dataset('air_temperature')\n",
"ds"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"dr = ds['air']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "97cf2e60189f42cf9dbb84494cc1a8a5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=0, description='itime', max=2919), Output()), _dom_classes=('widget-inte…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@interact(t=IntSlider(min=0,max=2919,step=1,value=0, \n",
" continuous_update=True,description='itime'))\n",
"def plot_layer(t):\n",
" plt.figure(figsize=[8, 8])\n",
" ax = plt.axes(projection=ccrs.PlateCarree())\n",
" dr[t].plot.pcolormesh(ax=ax, vmin=230, vmax=300, cbar_kwargs={'shrink': 0.4});\n",
" ax.coastlines();"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dr_sub = dr.sel(lon=slice(250,300), lat=slice(60, 30))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4bbafd155297487191fa9c49e7474c58",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=0, description='itime', max=2919), Output()), _dom_classes=('widget-inte…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@interact(t=IntSlider(min=0,max=2919,step=1,value=0, \n",
" continuous_update=True,description='itime'))\n",
"def plot_subset(t):\n",
" plt.figure(figsize=[8, 8])\n",
" ax = plt.axes(projection=ccrs.PlateCarree())\n",
" dr_sub[t].plot.pcolormesh(ax=ax, vmin=230, vmax=300, cbar_kwargs={'shrink': 0.5});\n",
" ax.coastlines();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment