{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Response of Tropical Cyclones to Volcanic Eruptions\n", "\n", "* Wenchang Yang (wenchang@princeton.edu)\n", "* Department of Geosciences, Princeton University" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:04:17.352183Z", "start_time": "2019-05-20T16:04:17.340306Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "**2019-05-20T12:04:17.346638**\n", ">>> Importing Python 3.7.3 packages...\n", "[OK]: import sys, os, os.path, datetime, glob\n", "[OK]: import numpy as np-1.16.3\n", "[OK]: import matplotlib as mpl-3.0.3; backend: module://ipykernel.pylab.backend_inline\n", "[OK]: #---import matplotlib.pyplot as plt\n", "[OK]: #---from pylab import *\n", "[OK]: import xarray as xr-0.12.1\n", "[OK]: #---import netCDF4\n", "[OK]: #---import dask\n", "[OK]: #---import bottleneck\n", "[OK]: import pandas as pd-0.24.2\n", "[OK]: from mpl_toolkits.basemap import Basemap\n", " PROJ_LIB = /scratch/gpfs/GEOCLIM/wenchang/miniconda3/envs/geoclim/share/proj\n", ">>>Import packages from Wenchang Yang (wython)...\n", "[OK]: import geoplots as gt\n", "[OK]: from geoplots import geoplot, fxyplot, mapplot, xticksyear\n", "[OK]: import geoxarray\n", "[OK]: import filter\n", "[OK]: import xlearn\n", "[OK]: import mysignal as sig\n", "**Done**\n" ] } ], "source": [ "%run -im pythonstartup\n", "\n", "%matplotlib notebook\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "plt.rcParams['hatch.color']='g'\n", "import xarray as xr\n", "import pandas as pd\n", "\n", "import geoxarray\n", "from geoplots import mapplot, yticks2lat\n", "\n", "from itertools import product\n", "from cftime import DatetimeJulian\n", "import xarray as xr\n", "import cartopy.crs as ccrs\n", "\n", "from mystats import p2t" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:12:24.119784Z", "start_time": "2019-05-20T16:12:24.028651Z" } }, "outputs": [], "source": [ "import regionmask" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:12:35.119606Z", "start_time": "2019-05-20T16:12:35.116396Z" } }, "outputs": [], "source": [ "def lon_shift(da):\n", " '''shift central longitude'''\n", " return da.roll(lon=da.lon.size//2).pipe(lambda x: x.assign_coords(lon=x.lon-(x.lon>180)*360))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:12:36.772692Z", "start_time": "2019-05-20T16:12:36.768712Z" } }, "outputs": [], "source": [ "def axscale(ax=None, right=0, left=0, up=0, down=0):\n", " '''scale the axes by the four sides'''\n", " if ax is None:\n", " ax = plt.gca()\n", " x0, y0, w, h = ax.get_position().bounds\n", " w_new = w*(1 + right + left)\n", " h_new = h*(1 + up + down)\n", " x0_new = x0 - w*left\n", " y0_new = y0 - h*down\n", " ax.set_position([x0_new, y0_new, w_new, h_new])\n", " return ax" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:12:40.707318Z", "start_time": "2019-05-20T16:12:40.617190Z" }, "code_folding": [ 99 ] }, "outputs": [], "source": [ "def get_tc_density(ifile, mon_start=None, mon_end=None):\n", " '''obtain TC # within the specified period (mon_start, mon_end) given the tracks file'''\n", "# ifile = None\n", "# ifile = '/tigress/wenchang/MODEL_OUT/CTL1860_noleap_tigercpu_intelmpi_18_576PE/analysis_lmh/cyclones_gav_ro110_1C_330k/atmos_11_11/Harris.TC/lmh_TCtrack_ts_4x.dat.warm.h29_25.TS.world.20110101-20120101.txt'\n", "\n", "# names = None\n", " names = ('time', 'lon', 'lat', 'slp', 'maxSpeed', 'tm')\n", "\n", "# mon_start = None\n", "# mon_end = 6\n", "\n", " lon_edges = None\n", " if lon_edges is None:\n", " lon_edges = np.arange(0,361)\n", " lon_centers = ( lon_edges[0:-1] + lon_edges[1:] )/2\n", " lon_range = (lon_edges[0], lon_edges[-1])\n", "\n", " lat_edges = None\n", " if lat_edges is None:\n", " lat_edges = np.arange(-60,61)\n", " lat_centers = ( lat_edges[0:-1] + lat_edges[1:] )/2\n", " lat_range = (lat_edges[0], lat_edges[-1])\n", "\n", "\n", " # use pandas to read the txt file\n", " df = pd.read_csv(ifile, sep='\\s+', names=names)\n", " # remove lines of track headers, i.e. lines with '+++'\n", " L = ['+++' not in t for t in df.time]\n", " df = df[L]\n", "\n", " # select date range if specified\n", " t0 = df.time.iloc[0]\n", " year = t0[0:4]\n", " if mon_start is not None:\n", " t_start = f'{year}{mon_start:02d}0100'\n", " df = df[df.time>t_start]\n", " if mon_end is not None:\n", " t_end = f'{year}{mon_end:02d}0100'\n", " df = df[df.time<=t_end]\n", "\n", " # use numpy.histgram2d to count TC #\n", " H, _dump1, _dump2 = np.histogram2d(df.lat, df.lon, \n", " bins=[lat_edges, lon_edges],\n", " range=[lat_range, lon_range]\n", " ) \n", " # wrap to xarray.DataArray\n", " H = xr.DataArray(H, dims=['lat', 'lon'], coords=[lat_centers, lon_centers])\n", " \n", " return H\n", "\n", "def lowpass_tc(H):\n", " '''rolling sum over lat and lon'''\n", " return H.rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "\n", "def tcplot(H, tag=None, **kws):\n", " cmap = kws.pop('cmap', 'OrRd')\n", " if tag is None:\n", " tag = ''\n", " plt.figure(figsize=(8,2.5))\n", "# H = H/4 # 4xdaily\n", " H.pipe(lowpass_tc).rename(f'TC days per year') \\\n", " .geo.cartoplot(proj='cyl', proj_kws={'central_longitude': 180}, cmap=cmap, **kws)\n", " plt.title(tag)\n", "\n", " plt.tight_layout()\n", "\n", "# control runs\n", "def ctl_tc_density_make():\n", " time_new = xr.cftime_range('0001-01', '0034-12', freq='MS', calendar='noleap')\n", " years = np.arange(11, 45)\n", " month_starts = [None,] + list(range(2, 13))\n", " month_ends = month_starts[1:] + [None,]\n", " das = []\n", " print('years')\n", " for year in years:\n", " print(year, end='; ')\n", " for mon_start,mon_end in zip(month_starts, month_ends):\n", " ifile = glob.glob(f'/tigress/wenchang/MODEL_OUT/CTL1860_noleap_tigercpu_intelmpi_18_576PE/analysis_lmh/cyclones_gav_ro110_1C_330k/atmos_{year}_{year}/Harris.TC/lmh_TCtrack_ts_4x.dat.warm.h29_25.TS.world.*-*.txt')[0]\n", " das.append(get_tc_density(ifile, mon_start=mon_start, mon_end=mon_end))\n", " print()\n", " da = xr.concat(das, dim=pd.Index(time_new, name='time'))\n", "\n", " # ctl -> ctl_ens\n", " ens = np.arange(1, 31)\n", " time_new = xr.cftime_range('0001-01', '0005-12', freq='MS', calendar='noleap')\n", " das = []\n", " print('ens')\n", " for en in ens:\n", " print(en, end='; ')\n", " year = en\n", " tspan = slice(f'{year:04d}-01', f'{year+4:04d}-12')\n", " das.append(da.sel(time=tspan).assign_coords(time=time_new))\n", " da = xr.concat(das, dim=pd.Index(ens, name='en'))\n", " print()\n", " \n", " return da\n", "\n", "# Volcanic runs\n", "def volc_tc_density_make(volc, year_erupt):\n", " '''construct tc density from the volc experiments'''\n", "# volc = 'Agung'\n", "# year_erupt = 1963\n", " time_new = xr.cftime_range(f'{year_erupt}-01', periods=12*5, freq='MS', calendar='noleap')\n", " ens = np.arange(1, 31)\n", " years = time_new.year[0::12]\n", " month_starts = [None,] + list(range(2, 13))\n", " month_ends = month_starts[1:] + [None,]\n", " das_ens = []\n", " print('ens:')\n", " for en in ens:\n", " print(en, end='; ')\n", " das = []\n", " for year in years:\n", " for mon_start,mon_end in zip(month_starts, month_ends):\n", " ifile = glob.glob(f'/tigress/wenchang/MODEL_OUT/{volc}_PI_ens_noleap/en{en:02d}/analysis_lmh/cyclones_gav_ro110_1C_330k/atmos_{year}_{year}/Harris.TC/lmh_TCtrack_ts_4x.dat.warm.h29_25.TS.world.*-*.txt')[0]\n", " das.append(get_tc_density(ifile, mon_start=mon_start, mon_end=mon_end))\n", " da = xr.concat(das, dim=pd.Index(time_new, name='time'))\n", " das_ens.append(da)\n", " da = xr.concat(das_ens, dim=pd.Index(ens, name='en'))\n", " print()\n", " \n", " return da" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:12:51.322814Z", "start_time": "2019-05-20T16:12:44.789414Z" }, "code_folding": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "years\n", "11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41; 42; 43; 44; \n", "ens\n", "1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; \n" ] } ], "source": [ "# data ctl_ens\n", "ctl_ens = ctl_tc_density_make()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:13:02.400991Z", "start_time": "2019-05-20T16:13:02.339133Z" } }, "outputs": [], "source": [ "# save to ncfile\n", "ofile = 'data/TC_Hist2D_ctl_ens.nc'\n", "if not os.path.exists(ofile):\n", " ctl_ens.to_dataset(name='TC_Hist2D').to_netcdf(ofile)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:13:04.024200Z", "start_time": "2019-05-20T16:13:03.895844Z" } }, "outputs": [], "source": [ "# ctl ens-mean\n", "ctl_emean = ctl_ens.sel(time='0001').mean('en').mean('time').pipe(lambda x: x*12/4).pipe(lowpass_tc)\n", "# ctl zonal-mean\n", "ctl_zmean = ctl_emean.mean('lon')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T16:15:37.285706Z", "start_time": "2019-05-20T16:15:37.129408Z" } }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# TC density in control run\n", "fig, axes = plt.subplots(1, 2, figsize=(8,3), sharey=True)\n", "da = ctl_ens\n", "levels = 2**np.arange(-2, 7).astype('float')\n", "cbar_kws = dict(ticks=[0.25, 1, 4, 16, 64], format='%.2g')\n", "\n", "ax = axes[0]\n", "ctl_zmean.plot(ax=ax, y='lat', color='C1')\n", "plt.autoscale()\n", "ax.set_ylabel('lat')\n", "ax.set_xlabel('Zonal mean TC density')\n", "ax.set_title('(a)', loc='left')\n", "\n", "ax = axes[1]\n", "\n", "ctl_emean.rename('TC days per year') \\\n", " .where(tcmask).plot.contourf(ax=ax, robust=True, levels=levels, cbar_kwargs=cbar_kws, cmap='Oranges')\n", "mapplot(coastlines_width=1/2)\n", "plt.ylim(-50, 50)\n", "plt.ylabel('')\n", "ax.set_yticks(range(-45, 46, 15))\n", "ax.set_yticklabels([f'{-n}$^\\circ$S' for n in range(-45,0,15)]\n", " + ['0']\n", " + [f'{n}$^\\circ$S' for n in range(15, 46, 15)])\n", "ax.set_title('(b)', loc='left')\n", "\n", "\n", "\n", "plt.tight_layout()\n", "axscale(axes[0], right=-0.5)\n", "axscale(axes[1], left=0.6)\n", "\n", "plt.savefig('figs/fig_tc_ctl.pdf')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### TC responses in volc experiments" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "ExecuteTime": { "end_time": "2019-04-22T15:48:56.200533Z", "start_time": "2019-04-22T15:48:48.348381Z" }, "code_folding": [ 0 ] }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# fig revision_1 spread 3x2 subplots\n", "fig, axes = plt.subplots(3, 2, figsize=(8, 8), sharey=True, sharex='col')\n", "n_years = 2\n", "lv = 2**np.arange(-3, 4.1)\n", "levels = list(-lv[-1::-1]) + [0,] + list(lv)\n", "coastline_width = .5\n", "cbar_ticks = (-16, -4, -1, -0.25, 0, 0.25, 1, 4, 16)\n", "cbar_format = '%.2g'\n", "alpha=0.2\n", "hatches = ['//////']\n", "\n", "ax = axes[0, 0]\n", "volc_name, tag, mon_start = 'Pinatubo', 'Pinatubo', 6\n", "color = 'k'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='k')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color=color, alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(a) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "\n", "ax = axes[1, 0]\n", "# plt.sca(ax)\n", "# ax.set_aspect(aspect)\n", "# plt.sca(ax)\n", "volc_name, tag, mon_start = 'Agung', 'Agung', 3\n", "color = 'C0'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C0')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C0', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(c) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[2, 0]\n", "volc_name, tag, mon_start = 'StMaria', 'StMaria', 10\n", "color = 'C1'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C1')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C1', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(e) {tag}', loc='left')\n", "ax.set_xlabel('Zonal mean TC density response')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[0, 1]\n", "volc_name, mon_start = 'Pinatubo', 6\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(b)', loc='left')\n", "# expand_ax(ax)\n", "ax.text(1, 1.02, f'n_years = {n_years}', \n", " transform=ax.transAxes, ha='right', va='bottom')\n", "\n", "ax = axes[1, 1]\n", "volc_name, mon_start = 'Agung', 3\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).rename('TC days per year').plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(d)', loc='left')\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "ax = axes[2, 1]\n", "plt.sca(ax)\n", "volc_name, mon_start = 'StMaria', 10\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "ax.set_yticks(np.arange(-45,46,15))\n", "ax.set_yticklabels(['45$^\\circ$S', '30$^\\circ$S', '15$^\\circ$S', '0', '15$^\\circ$N', '30$^\\circ$N', '45$^\\circ$N'])\n", "plt.title('(f)', loc='left')\n", "# yticks2lat(np.arange(-45,46,15))\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "plt.tight_layout()\n", "for ax in axes[:, 0]:\n", " axscale(ax, right=-0.5)\n", "for ax in axes[:, 1]:\n", " axscale(ax, left=0.6)\n", "\n", "figname = f'figs/fig_tc_nyears{n_years}.pdf'\n", "plt.savefig(figname)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### iyear=1,2,3" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T18:55:47.046837Z", "start_time": "2019-05-20T18:55:43.154884Z" }, "code_folding": [ 0 ] }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# fig revision_2 spread 3x2 subplots, iyear=2\n", "fig, axes = plt.subplots(3, 2, figsize=(8, 8), sharey=True, sharex='col')\n", "iyear = 2\n", "lv = 2**np.arange(-3, 4.1)\n", "levels = list(-lv[-1::-1]) + [0,] + list(lv)\n", "coastline_width = .5\n", "cbar_ticks = (-16, -4, -1, -0.25, 0, 0.25, 1, 4, 16)\n", "cbar_format = '%.2g'\n", "alpha=0.2\n", "hatches = ['//////']\n", "\n", "ax = axes[0, 0]\n", "volc_name, tag, mon_start = 'Pinatubo', 'Pinatubo', 6\n", "color = 'k'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='k')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color=color, alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(a) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "\n", "ax = axes[1, 0]\n", "# plt.sca(ax)\n", "# ax.set_aspect(aspect)\n", "# plt.sca(ax)\n", "volc_name, tag, mon_start = 'Agung', 'Agung', 3\n", "color = 'C0'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C0')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C0', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(c) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[2, 0]\n", "volc_name, tag, mon_start = 'StMaria', 'StMaria', 10\n", "color = 'C1'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C1')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C1', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(e) {tag}', loc='left')\n", "ax.set_xlabel('Zonal mean TC density response')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[0, 1]\n", "volc_name, mon_start = 'Pinatubo', 6\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(b)', loc='left')\n", "# expand_ax(ax)\n", "ax.text(1, 1.02, f'Year {iyear}', \n", " transform=ax.transAxes, ha='right', va='bottom')\n", "\n", "ax = axes[1, 1]\n", "volc_name, mon_start = 'Agung', 3\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).rename('TC days per year').plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(d)', loc='left')\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "ax = axes[2, 1]\n", "plt.sca(ax)\n", "volc_name, mon_start = 'StMaria', 10\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "ax.set_yticks(np.arange(-45,46,15))\n", "ax.set_yticklabels(['45$^\\circ$S', '30$^\\circ$S', '15$^\\circ$S', '0', '15$^\\circ$N', '30$^\\circ$N', '45$^\\circ$N'])\n", "plt.title('(f)', loc='left')\n", "# yticks2lat(np.arange(-45,46,15))\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "plt.tight_layout()\n", "for ax in axes[:, 0]:\n", " axscale(ax, right=-0.5)\n", "for ax in axes[:, 1]:\n", " axscale(ax, left=0.6)\n", "\n", "figname = f'figs/fig_tc_year{iyear}.pdf'\n", "plt.savefig(figname)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2019-05-20T18:57:22.515661Z", "start_time": "2019-05-20T18:57:18.815567Z" }, "code_folding": [ 0 ] }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# fig revision_2 spread 3x2 subplots, iyear=4\n", "fig, axes = plt.subplots(3, 2, figsize=(8, 8), sharey=True, sharex='col')\n", "iyear = 4\n", "lv = 2**np.arange(-3, 4.1)\n", "levels = list(-lv[-1::-1]) + [0,] + list(lv)\n", "coastline_width = .5\n", "cbar_ticks = (-16, -4, -1, -0.25, 0, 0.25, 1, 4, 16)\n", "cbar_format = '%.2g'\n", "alpha=0.2\n", "hatches = ['//////']\n", "\n", "ax = axes[0, 0]\n", "volc_name, tag, mon_start = 'Pinatubo', 'Pinatubo', 6\n", "color = 'k'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='k')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color=color, alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(a) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "\n", "ax = axes[1, 0]\n", "# plt.sca(ax)\n", "# ax.set_aspect(aspect)\n", "# plt.sca(ax)\n", "volc_name, tag, mon_start = 'Agung', 'Agung', 3\n", "color = 'C0'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C0')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C0', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(c) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[2, 0]\n", "volc_name, tag, mon_start = 'StMaria', 'StMaria', 10\n", "color = 'C1'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)\n", "ts = ts.mean('en')\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C1')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C1', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(e) {tag}', loc='left')\n", "ax.set_xlabel('Zonal mean TC density response')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[0, 1]\n", "volc_name, mon_start = 'Pinatubo', 6\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(b)', loc='left')\n", "# expand_ax(ax)\n", "\n", "ax = axes[1, 1]\n", "volc_name, mon_start = 'Agung', 3\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).rename('TC days per year').plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(d)', loc='left')\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "ax = axes[2, 1]\n", "plt.sca(ax)\n", "volc_name, mon_start = 'StMaria', 10\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1 + (iyear-1)*12, mon_start-1 + iyear*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)\n", "da = da.mean('en')\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "ax.set_yticks(np.arange(-45,46,15))\n", "ax.set_yticklabels(['45$^\\circ$S', '30$^\\circ$S', '15$^\\circ$S', '0', '15$^\\circ$N', '30$^\\circ$N', '45$^\\circ$N'])\n", "plt.title('(f)', loc='left')\n", "# yticks2lat(np.arange(-45,46,15))\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "plt.tight_layout()\n", "for ax in axes[:, 0]:\n", " axscale(ax, right=-0.5)\n", "for ax in axes[:, 1]:\n", " axscale(ax, left=0.6)\n", "\n", "figname = f'figs/fig_tc_year{iyear}.pdf'\n", "# plt.savefig(figname)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "ExecuteTime": { "end_time": "2019-05-21T17:49:18.291772Z", "start_time": "2019-05-21T17:49:09.578803Z" }, "code_folding": [] }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# fig revision_1 spread 3x2 subplots relative both\n", "fig, axes = plt.subplots(3, 2, figsize=(8, 8), sharey=True, sharex='col')\n", "n_years = 3\n", "lv = 2**np.arange(-3, 4.1, 0.5)\n", "levels = np.arange(-50,51,10)#list(-lv[-1::-1]) + [0,] + list(lv)\n", "coastline_width = .5\n", "cbar_ticks = None#(-16, -4, -1, -0.25, 0, 0.25, 1, 4, 16)\n", "cbar_format = None#'%.2g'\n", "alpha=0.2\n", "hatches = ['//////']\n", "\n", "ax = axes[0, 0]\n", "volc_name, tag, mon_start = 'Pinatubo', 'Pinatubo', 6\n", "color = 'k'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)/ctl_zmean*100\n", "ts = ts.mean('en')/ctl_zmean*100\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='k')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color=color, alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(a) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(-10, color='gray', lw=1, ls='--')\n", "ax.axvline(10, color='gray', lw=1, ls='--')\n", "\n", "\n", "ax = axes[1, 0]\n", "# plt.sca(ax)\n", "# ax.set_aspect(aspect)\n", "# plt.sca(ax)\n", "volc_name, tag, mon_start = 'Agung', 'Agung', 3\n", "color = 'C0'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)/ctl_zmean*100\n", "ts = ts.mean('en')/ctl_zmean*100\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C0')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C0', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(c) {tag}', loc='left')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(-10, color='gray', lw=1, ls='--')\n", "ax.axvline(10, color='gray', lw=1, ls='--')\n", "\n", "ax = axes[2, 0]\n", "volc_name, tag, mon_start = 'StMaria', 'StMaria', 10\n", "color = 'C1'\n", "ts = dvs[volc_name].pipe(lambda x: x-ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True).sum() \\\n", " .rolling(lat=10, center=True).sum() \\\n", " .mean('lon')\n", "spread = p2t(0.05, df=ts.en.size-1) * ts.std('en') * ts.en.size**(-1/2)/ctl_zmean*100\n", "ts = ts.mean('en')/ctl_zmean*100\n", "ts.plot(ax=ax, y='lat', label=tag, lw=1.5, color='C1')\n", "ax.fill_betweenx(y=ts.lat, x1=ts-spread, x2=ts+spread, color='C1', alpha=alpha)\n", "ax.autoscale()\n", "ax.set_ylim(-50, 50)\n", "ax.set_ylabel('')\n", "# ax.grid('on')\n", "ax.set_title(f'(e) {tag}', loc='left')\n", "ax.set_xlabel('Zonal mean response [%]')\n", "ax.set_yticks(range(-45,46,15))\n", "ax.axhline(0, color='gray', lw=1, ls='--')\n", "ax.axvline(0, color='gray', lw=1, ls='--')\n", "ax.set_xlim(-20,20)\n", "ax.axvline(-10, color='gray', lw=1, ls='--')\n", "ax.axvline(10, color='gray', lw=1, ls='--')\n", "\n", "\n", "ax = axes[0, 1]\n", "volc_name, mon_start = 'Pinatubo', 6\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)/ctl_emean*100\n", "da = da.mean('en')/ctl_emean*100\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).rename('%').plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(b)', loc='left')\n", "# expand_ax(ax)\n", "\n", "ax = axes[1, 1]\n", "volc_name, mon_start = 'Agung', 3\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)/ctl_emean*100\n", "da = da.mean('en')/ctl_emean*100\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).rename('%').plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "plt.title('(d)', loc='left')\n", "# expand_ax(ax)\n", "\n", "\n", "\n", "ax = axes[2, 1]\n", "plt.sca(ax)\n", "volc_name, mon_start = 'StMaria', 10\n", "da = dvs[volc_name].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon_start-1, mon_start-1 + n_years*12)).mean('time').pipe(lambda x: x*12/4) \\\n", " .rolling(lon=10, center=True, min_periods=1).sum() \\\n", " .rolling(lat=10, center=True, min_periods=1).sum()\n", "spread = p2t(0.05, df=da.en.size-1) * da.std('en') * da.en.size**(-1/2)/ctl_emean*100\n", "da = da.mean('en')/ctl_emean*100\n", "sigmask = np.abs(da) > spread\n", "bothmask = sigmask & tcmask\n", "da.where(tcmask).rename('%').plot(ax=ax, levels=levels, cbar_kwargs={'ticks': cbar_ticks, 'format': cbar_format}, rasterized=True)\n", "da.where(bothmask).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=hatches, \n", " add_colorbar=False)\n", "mapplot(ax=ax, lon=da.lon, lat=(-50, 50), linewidth=coastline_width)\n", "ax.set_xlabel('')\n", "ax.set_ylabel('')\n", "ax.set_yticks(np.arange(-45,46,15))\n", "ax.set_yticklabels(['45$^\\circ$S', '30$^\\circ$S', '15$^\\circ$S', '0', '15$^\\circ$N', '30$^\\circ$N', '45$^\\circ$N'])\n", "plt.title('(f)', loc='left')\n", "\n", "\n", "\n", "plt.tight_layout()\n", "for ax in axes[:, 0]:\n", " axscale(ax, right=-0.5)\n", "for ax in axes[:, 1]:\n", " axscale(ax, left=0.6)\n", "axes[2,0].set_xticks(np.arange(-20,21,10))\n", "\n", "figname = 'figs/fig_tc_percent_both.pdf'\n", "plt.savefig(figname)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basin-mean TC resopnses" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2019-04-17T19:49:43.880531Z", "start_time": "2019-04-17T19:49:23.182736Z" }, "code_folding": [ 0 ], "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pinatubo 6 JUN\n", "NA; EP; WP; NI; SI; AU; SP; SA; \n", "Agung 3 MAR\n", "NA; EP; WP; NI; SI; AU; SP; SA; \n", "StMaria 10 OCT\n", "NA; EP; WP; NI; SI; AU; SP; SA; \n" ] } ], "source": [ "# cal tc basin mean\n", "n_years = 4\n", "time_new = np.arange(1, n_years+1)\n", "dass = []\n", "spreadss = []\n", "volcs = ['Pinatubo', 'Agung', 'StMaria']\n", "mon_starts = [6, 3, 10]\n", "mon_names = ['JUN', 'MAR', 'OCT']\n", "name = 'TCBasins'\n", "names = ['North Atlantic', 'East Pacific', 'West Pacific', 'North Indian',\n", " 'South Indian', 'Australia', 'South Pacific', 'South Atlantic']\n", "abbrevs = ['NA', 'EP', 'WP', 'NI',\n", " 'SI', 'AU', 'SP', 'SA']\n", "lat1 = 40\n", "outlines = [( (295, 0), (260, 20), (260, lat1), (360, lat1), (360, 0) ),\n", " ( (200, 0), (200, lat1), (260, lat1), (260, 20), (295, 0) ), \n", " ( (105, 0), (105, lat1), (200, lat1), (200, 0) ),\n", " ( (30, 0), (30, lat1), (105, lat1), (105, 0) ),\n", " ( (30, 0), (30, -lat1), (105, -lat1), (105, 0) ),\n", " ( (105,0), (105, -lat1), (165, -lat1), (165, 0) ),\n", " ( (165, 0), (165, -lat1), (290, -lat1), (290, 0) ),\n", " ( (290, 0), (290, -lat1), (360, -lat1), (360, 0) ),\n", " ]\n", "numbers = np.arange(len(names))\n", "basins = regionmask.Regions_cls(name=name, numbers=numbers, names=names, abbrevs=abbrevs, outlines=outlines)\n", "bnumbers = basins.numbers\n", "lat, lon = ctl_ens.lat, ctl_ens.lon\n", "basinmasks = basins.mask(lon, lat)\n", "for volc,mon,mon_name in zip(volcs, mon_starts, mon_names):\n", " print(volc, mon, mon_name)\n", " das = []\n", " spreads = []\n", " for bnumber in bnumbers:\n", " print(basins.abbrevs[bnumber], end='; ')\n", " da = dvs[volc].pipe(lambda x: x - ctl_ens.assign_coords(time=x.time)) \\\n", " .isel(time=slice(mon-1, mon-1+n_years*12)) \\\n", " .resample(time=f'AS-{mon_name}').mean('time').pipe(lambda x: x*12/4) \\\n", " .pipe(lowpass_tc).where(basinmasks==bnumber).where(np.abs(lat)');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# fig basins\n", "plt.figure(figsize=(8,4))\n", "dproj = ccrs.PlateCarree()\n", "bbox = {'facecolor': 'w', 'alpha':.8, 'boxstyle': 'round', 'edgecolor': 'none'}\n", "tcolor = 'C0'\n", "p = basinmasks.geo.cartoplot(proj='moll', \n", " proj_kws={'central_longitude': 195}, \n", " rasterized=True,\n", " add_colorbar=False)\n", "ax = p.axes\n", "basins.plot(ax=ax, label='abbrev',\n", " add_ocean=False, line_kws={'color': 'lightgray'})\n", "# ax.set_global()\n", "\n", "ax.text(30-3, 0, '0$^\\circ$', transform=dproj, ha='right', va='center', bbox=bbox, color=tcolor)\n", "ax.text(30-10, 40, '40$^\\circ$N', transform=dproj, ha='right', va='center', bbox=bbox, color=tcolor)\n", "ax.text(30-10, -40, '40$^\\circ$S', transform=dproj, ha='right', va='center', bbox=bbox, color=tcolor)\n", "ax.text(30, -40-3, '30$^\\circ$E', transform=dproj, ha='center', va='top', bbox=bbox, color=tcolor)\n", "ax.text(105, -40-3, '105$^\\circ$E', transform=dproj, ha='center', va='top', bbox=bbox, color=tcolor)\n", "ax.text(165, -40-3, '165$^\\circ$E', transform=dproj, ha='center', va='top', bbox=bbox, color=tcolor)\n", "ax.text(200, 0-3, f'{360-200}$^\\circ$W', transform=dproj, ha='center', va='top', bbox=bbox, color=tcolor)\n", "ax.text(290, -40-3, f'{360-290}$^\\circ$W', transform=dproj, ha='center', va='top', bbox=bbox, color=tcolor)\n", "ax.text(295+2, 0-3, f'{360-295}$^\\circ$W', transform=dproj, ha='left', va='top', bbox=bbox, color=tcolor)\n", "ax.text(360, -40-3, '0$^\\circ$', transform=dproj, ha='center', va='top', bbox=bbox, color=tcolor)\n", "ax.text(260+2, 20-2, f'{360-260}$^\\circ$W,20$^\\circ$N', transform=dproj, ha='left', va='top', bbox=bbox, color=tcolor)\n", "\n", "ax.set_title('Global TC Basins')\n", "plt.tight_layout()\n", "\n", "figname = 'figs/fig_tc_basins.pdf'\n", "plt.savefig(figname)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## test" ] }, { "cell_type": "code", "execution_count": 177, "metadata": { "ExecuteTime": { "end_time": "2019-04-15T02:49:46.395779Z", "start_time": "2019-04-15T02:49:46.360494Z" } }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grids = region.mask(da.lon, da.lat)\n", "\n", "grids.plot()" ] }, { "cell_type": "code", "execution_count": 172, "metadata": { "ExecuteTime": { "end_time": "2019-04-13T02:59:48.544511Z", "start_time": "2019-04-13T02:59:48.510102Z" } }, "outputs": [], "source": [ "# N Atlantic mask\n", "bname, blongname = 'NAtl', 'North Atlantic'\n", "path = [(295, 0), (260, 20), (260, 50), (360, 50), (360, 0)]\n", "region = regionmask.Regions_cls(bname, [0], [blongname], [bname], [path])\n", "basinmask = region.mask(ctl_ens.lon, ctl_ens.lat)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": { "ExecuteTime": { "end_time": "2019-04-13T03:01:16.764973Z", "start_time": "2019-04-13T03:01:14.103599Z" }, "code_folding": [ 0 ] }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = 'TCBasins'\n", "names = ['North Atlantic', 'East Pacific', 'West Pacific', 'North Indian',\n", " 'South Indian', 'Australia', 'South Pacific', 'South Atlantic']\n", "abbrevs = ['NA', 'EP', 'WP', 'NI',\n", " 'SI', 'AU', 'SP', 'SA']\n", "outlines = [( (295, 0), (260, 20), (260, 90), (360, 90), (360, 0) ),\n", " ( (200, 0) ), (200,90), (260,90), (260,20), (295, 0), \n", " ( (105, 0), (105, 90), (200, 90), (200, 0) ),\n", " ( (30, 0), (30, 90), (105, 90), (105, 0) ),\n", " ( (30, 0), (30, -90), (105, -90), (105, 0) ),\n", " ( (105,0), (105, -90), (165, -90), (165, 0) ),\n", " ( (165, 0), (165, -90), (290, -90), (290, 0) ),\n", " \n", " ( (290, 0), (290, -90), (360, -90), (360, 0) ),\n", " ]\n", "numbers = np.arange(len(names))\n", "basins = regionmask.Regions_cls(name=name, numbers=numbers, names=names, abbrevs=abbrevs, outlines=outlines)\n", "\n", "basins.plot(label='abbrev', proj=ccrs.PlateCarree(central_longitude=180))" ] }, { "cell_type": "code", "execution_count": 185, "metadata": { "ExecuteTime": { "end_time": "2019-04-15T18:06:39.788761Z", "start_time": "2019-04-15T18:06:39.590004Z" } }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# fig errorbar NAtl\n", "fig, ax = plt.subplots()\n", "\n", "lon, lat = ctl_ens.lon, ctl_ens.lat\n", "n_years = 4\n", "time_new = np.arange(1, n_years+1)\n", "csize = 5 #capsize \n", "fmt = 'o'\n", "lat1 = 40\n", "\n", "def tc_errorbar(ax=None, bnumber=0):\n", " '''plot errorbar of TC density over a specified basin'''\n", " if ax is None:\n", " ax = plt.gca()\n", "# bnumber = 0\n", " bname = basins.names[bnumber]\n", " ctl_bmean = ctl_ens.sel(time='0001').mean('en').mean('time').pipe(lambda x: x*12/4) \\\n", " .pipe(lowpass_tc).where(basinmasks==bnumber).where(np.abs(lat)');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "da = ctl_ens\n", "da.sel(time='0001').mean('en').mean('time').pipe(lambda x: x*12/4) \\\n", " .pipe(lambda x: x.where(x>0)).plot()\n", "mapplot()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2019-04-12T17:39:11.524878Z", "start_time": "2019-04-12T17:39:11.385441Z" } }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "da = ctl_ens\n", "da.sel(time='0001').mean('en').mean('time').pipe(lambda x: x*12/4) \\\n", " .pipe(lambda x: x.where(x>0)).pipe(lowpass_tc).plot()\n", "mapplot()" ] }, { "cell_type": "code", "execution_count": 336, "metadata": { "ExecuteTime": { "end_time": "2019-04-05T16:10:37.909446Z", "start_time": "2019-04-05T16:10:37.903646Z" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] } ], "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.7.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }