{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sri Lanka Rainfall response to ENSO\n", "* Wenchang Yang (wenchang@prnceton.edu)\n", "* Department of Geosciences, Princeton University" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:22:12.926020Z", "start_time": "2019-03-30T02:22:12.913981Z" }, "code_folding": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "**2019-03-29T22:22:12.920343**\n", ">>> Importing Python 3.7.2 packages...\n", "[OK]: import sys, os, os.path, datetime, glob\n", "[OK]: import numpy as np-1.16.2\n", "[OK]: import matplotlib as mpl-3.0.3; backend: nbAgg\n", "[OK]: #---import matplotlib.pyplot as plt\n", "[OK]: #---from pylab import *\n", "[OK]: import xarray as xr-0.12.0\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 = /tigress/wenchang/miniconda3p7/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": [ "# init\n", "%run -im pythonstartup" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-03-29T21:02:20.162034Z", "start_time": "2019-03-29T21:02:19.984820Z" } }, "outputs": [], "source": [ "%matplotlib notebook" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-03-29T21:02:21.465774Z", "start_time": "2019-03-29T21:02:21.307712Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "

Client

\n", "\n", "
\n", "

Cluster

\n", "
    \n", "
  • Workers: 4
  • \n", "
  • Cores: 16
  • \n", "
  • Memory: 162.00 GB
  • \n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# misc\n", "from dask.distributed import Client\n", "client = Client('127.0.0.1:8982')\n", "client" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-03-29T21:02:26.491395Z", "start_time": "2019-03-29T21:02:22.685315Z" } }, "outputs": [], "source": [ "from climindex import get_climate_index\n", "from mystats import p2t\n", "\n", "from misc.seasons import month2name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## data" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:22:27.743999Z", "start_time": "2019-03-30T02:22:26.358956Z" } }, "outputs": [], "source": [ "# data: nino34\n", "nino34 = get_climate_index('nino34')\n", "nino34 = nino34.to_xarray().rename({'index': 'time'}).resample(time='MS').mean('time')\n", "\n", "nino34_mon = nino34.isel(time=nino34.time.dt.month==12).sel(time=slice('1981', '2017'))\n", "\n", "#\n", "THD = 0.5\n", "years_nino = nino34_mon.isel(time=nino34_mon>=THD).time.dt.year.data\n", "print(years_nino.size, 'Nino years', years_nino)\n", "years_nina = nino34_mon.isel(time=nino34_mon<=-THD).time.dt.year.data\n", "print(years_nina.size, 'Nina years', years_nina)\n", "years_neut = nino34_mon.isel(time=(nino34_mon>-THD)&(nino34_mon\n", "array([[-1.632496, -0.797955, -1.295024, ..., -0.35249 , 2.399288, 2.259517],\n", " [-3.828342, -2.714553, -1.803307, ..., -4.510641, -4.111227, 5.586184],\n", " [-3.671874, -2.440988, -2.448466, ..., -3.768468, 2.390826, -4.217482],\n", " ...,\n", " [-3.703977, 0.530313, -0.245059, ..., -5.147067, -0.75407 , -5.069199],\n", " [-2.962908, -0.914233, -1.1707 , ..., -0.577097, 1.648988, -3.543369],\n", " [ 0.805361, -0.4302 , 2.84932 , ..., 3.962558, -0.964712, -2.142888]])\n", "Coordinates:\n", " * year (year) int64 1981 1982 1983 1984 1985 ... 2013 2014 2015 2016 2017\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# anomaly (time,) -> (year, month)\n", "years = np.arange(1981, 2018)\n", "months = np.arange(1, 25)\n", "n_months = months.size\n", "da = chirps_a\n", "_das = []\n", "for year in years:\n", " tspan = slice(f'{year}', f'{year+1}')\n", " _das.append(da.sel(time=tspan).data)\n", "da = xr.DataArray(_das, dims=['year', 'month'], coords=[years, months])\n", "anom = da\n", "da\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:26:02.948010Z", "start_time": "2019-03-30T02:26:02.914549Z" } }, "outputs": [], "source": [ "# data chirps normalized\n", "da = chirps_a\n", "da = da.groupby('time.month')/da.sel(time=tclim).groupby('time.month').std('time')\n", "chirps_z = da" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:26:14.121316Z", "start_time": "2019-03-30T02:26:14.066489Z" }, "code_folding": [] }, "outputs": [ { "data": { "text/plain": [ "\n", "array([[-0.631006, -0.302822, -0.511618, ..., -0.12684 , 0.610776, 0.642577],\n", " [-1.479762, -1.030166, -0.712423, ..., -1.623111, -1.046577, 1.588637],\n", " [-1.419283, -0.926349, -0.967302, ..., -1.356047, 0.608622, -1.199396],\n", " ...,\n", " [-1.431691, 0.201252, -0.096814, ..., -1.852123, -0.19196 , -1.441613],\n", " [-1.145247, -0.346949, -0.462502, ..., -0.207663, 0.419776, -1.007687],\n", " [ 0.311295, -0.16326 , 1.125665, ..., 1.425889, -0.245583, -0.609409]])\n", "Coordinates:\n", " * year (year) int64 1981 1982 1983 1984 1985 ... 2013 2014 2015 2016 2017\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# z value (time,) -> (year, month)\n", "years = np.arange(1981, 2018)\n", "months = np.arange(1, 25)\n", "n_months = months.size\n", "da = chirps_z\n", "_das = []\n", "for year in years:\n", " tspan = slice(f'{year}', f'{year+1}')\n", " _das.append(da.sel(time=tspan).data)\n", "da = xr.DataArray(_das, dims=['year', 'month'], coords=[years, months])\n", "z_value = da\n", "da\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FLOR" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2019-03-29T21:07:32.359955Z", "start_time": "2019-03-29T21:07:32.258185Z" } }, "outputs": [], "source": [ "# data flor nuged\n", "if 'flors' not in globals():\n", " flors = dict()\n", "func_scale = lambda x: x*24*3600\n", "ifile = 'data/flor.nudgeLongAll_newdiag_tigercpu_intelmpi_18_576PE.en01.p5.SriLanka.masked.nc'\n", "ds = xr.open_dataset(ifile)\n", "da = ds.precip.pipe(func_scale).geo.fldmean() \\\n", " .pipe(lambda x: x.groupby('time.month') - x.groupby('time.month').mean('time') )\n", "flors['pr_a'] = da" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:45:48.349832Z", "start_time": "2019-03-30T02:45:47.798349Z" } }, "outputs": [], "source": [ "# anomaly (time,) -> (year, month)\n", "years = np.arange(1881, 2016)\n", "months = np.arange(1, 25)\n", "n_months = months.size\n", "da = flors['pr_a']\n", "_das = []\n", "for year in years:\n", " tspan = slice(f'{year}', f'{year+1}')\n", " _das.append(da.sel(time=tspan).data)\n", "da = xr.DataArray(_das, dims=['year', 'month'], coords=[years, months])\n", "flors['pr_a_2d'] = da" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:43:35.560580Z", "start_time": "2019-03-30T02:43:35.484216Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "FlOR nudged experiment:\n", "39 Nino years [1885 1888 1895 1896 1899 1902 1904 1905 1911 1913 1914 1918 1923 1925\n", " 1930 1940 1941 1951 1957 1963 1965 1968 1969 1972 1976 1977 1979 1982\n", " 1986 1987 1991 1994 1997 2002 2004 2006 2009 2014 2015]\n", "39 Nina years [1881 1882 1886 1889 1890 1892 1893 1894 1897 1903 1906 1908 1909 1910\n", " 1916 1924 1933 1938 1942 1949 1950 1952 1954 1955 1956 1964 1970 1971\n", " 1973 1974 1975 1983 1984 1988 1998 1999 2007 2010 2011]\n", "57 Neutral years [1883 1884 1887 1891 1898 1900 1901 1907 1912 1915 1917 1919 1920 1921\n", " 1922 1926 1927 1928 1929 1931 1932 1934 1935 1936 1937 1939 1943 1944\n", " 1945 1946 1947 1948 1953 1958 1959 1960 1961 1962 1966 1967 1978 1980\n", " 1981 1985 1989 1990 1992 1993 1995 1996 2000 2001 2003 2005 2008 2012\n", " 2013]\n" ] } ], "source": [ "# flor nudged nino34\n", "ifile = 'data/flor.nudgeLongAll_newdiag_tigercpu_intelmpi_18_576PE.en01.p5.nino34.nc'\n", "ds = xr.open_dataset(ifile)\n", "da = ds.nino34.sel(time=slice('1881', '2015'))\n", "da = da.groupby('time.month') - da.groupby('time.month').mean('time')\n", "\n", "# dec\n", "nino34_mon = da.isel(time=da.time.dt.month==12)\n", "# nino34_mon.plot()\n", "\n", "#\n", "THD = 0.5\n", "print('FlOR nudged experiment:')\n", "flors['years_nino'] = nino34_mon.isel(time=nino34_mon>=THD).time.dt.year.data\n", "print(flors['years_nino'].size, 'Nino years', flors['years_nino'])\n", "flors['years_nina'] = nino34_mon.isel(time=nino34_mon<=-THD).time.dt.year.data\n", "print(flors['years_nina'].size, 'Nina years', flors['years_nina'])\n", "flors['years_neut'] = nino34_mon.isel(time=(nino34_mon>-THD)&(nino34_mon');\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" }, { "name": "stdout", "output_type": "stream", "text": [ "2.200985160082949\n", "\n", "array([1.996578, 1.400569, 2.004991, 1.276508, 1.66407 , 0.73554 , 0.595706,\n", " 1.081283, 2.077115, 2.482564, 4.093288, 4.145824, 2.276485, 1.054122,\n", " 1.236979, 2.786619, 3.216801, 0.952232, 1.063763, 1.105497, 2.052339,\n", " 2.991657, 4.302694, 4.317968])\n", "Coordinates:\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24\n", "0.28867513459481287\n", "\n", "array([1.268565, 0.889879, 1.273911, 0.811054, 1.0573 , 0.46734 , 0.378494,\n", " 0.687015, 1.319736, 1.577346, 2.600751, 2.634131, 1.446409, 0.669757,\n", " 0.785939, 1.770533, 2.043858, 0.605019, 0.675883, 0.702399, 1.303994,\n", " 1.900808, 2.733802, 2.743506])\n", "Coordinates:\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24\n" ] } ], "source": [ "# plot errorbar\n", "plt.figure(figsize=(7,4))\n", "da = anom\n", "lw = 2\n", "alpha = .8\n", "csize = 5\n", "\n", "# c = 'k'\n", "# s = 'Neutral'\n", "# _years = years_neut\n", "# spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "# da_mean = da.sel(year=_years).mean('year')\n", "# da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", "# color=c, alpha=alpha, capsize=csize, label=s\n", "# )\n", "\n", "c = 'C3'\n", "s = 'Nino'\n", "_years = years_nino\n", "spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "da_mean = da.sel(year=_years).mean('year')\n", "da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", " color=c, alpha=alpha, capsize=csize, label=s\n", " )\n", "print(p2t(0.05, df=_years.size-1))\n", "print(da.sel(year=_years).std('year'))\n", "print((_years.size)**(-1/2))\n", "print(spread)\n", "\n", "c = 'C0'\n", "s = 'Nina'\n", "_years = years_nina\n", "spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "da_mean = da.sel(year=_years).mean('year')\n", "da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", " color=c, alpha=alpha, capsize=csize, label=s\n", " )\n", "\n", "plt.axhline(0, color='gray', ls='--')\n", "plt.axvline(12.5, color='gray', ls='--')\n", "month_names = month2name(np.mod(da.month.data-1, 12) + 1)\n", "plt.xticks(da.month, month_names)\n", "plt.xlim(6-.2, 12+6+.2)\n", "plt.ylabel('Precip anomaly [mm/day]')\n", "plt.legend(frameon=True, loc='upper left')\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FLOR nudge" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T02:50:54.421862Z", "start_time": "2019-03-30T02:50:53.946414Z" }, "code_folding": [ 0 ], "scrolled": false }, "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" }, { "name": "stdout", "output_type": "stream", "text": [ "2.024394164575136\n", "\n", "array([2.213414, 1.714029, 1.911471, 2.09419 , 1.808866, 1.337897, 0.741476,\n", " 0.991855, 1.108175, 2.923555, 3.333091, 2.876098, 1.793997, 1.684493,\n", " 1.274851, 2.296057, 2.140041, 1.223836, 0.886691, 0.945346, 1.218546,\n", " 2.759526, 3.445474, 3.644168])\n", "Coordinates:\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24\n", "0.16012815380508713\n", "\n", "array([0.717506, 0.555624, 0.619627, 0.678858, 0.586366, 0.433696, 0.240359,\n", " 0.321522, 0.359229, 0.947707, 1.080463, 0.932323, 0.581547, 0.546049,\n", " 0.413259, 0.744296, 0.693721, 0.396722, 0.287432, 0.306446, 0.395007,\n", " 0.894535, 1.116894, 1.181303])\n", "Coordinates:\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24\n" ] } ], "source": [ "# plot errorbar nudge\n", "plt.figure(figsize=(7,4))\n", "expname = 'FLOR_nudge'\n", "da = flors['pr_a_2d']\n", "lw = 2\n", "alpha = .8\n", "csize = 5\n", "\n", "# c = 'k'\n", "# s = 'Neutral'\n", "# _years = years_neut\n", "# spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "# da_mean = da.sel(year=_years).mean('year')\n", "# da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", "# color=c, alpha=alpha, capsize=csize, label=s\n", "# )\n", "\n", "c = 'C3'\n", "s = 'Nino'\n", "_years = flors['years_nino']\n", "spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "da_mean = da.sel(year=_years).mean('year')\n", "da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", " color=c, alpha=alpha, capsize=csize, label=s\n", " )\n", "print(p2t(0.05, df=_years.size-1))\n", "print(da.sel(year=_years).std('year'))\n", "print((_years.size)**(-1/2))\n", "print(spread)\n", "\n", "\n", "c = 'C0'\n", "s = 'Nina'\n", "_years = flors['years_nina']\n", "spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "da_mean = da.sel(year=_years).mean('year')\n", "da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", " color=c, alpha=alpha, capsize=csize, label=s\n", " )\n", "\n", "plt.axhline(0, color='gray', ls='--')\n", "plt.axvline(12.5, color='gray', ls='--')\n", "month_names = month2name(np.mod(da.month.data-1, 12) + 1)\n", "plt.xticks(da.month, month_names)\n", "plt.xlim(6-.5, 6+12+.5)\n", "plt.ylabel('Precip anomaly normalized')\n", "plt.title(f'{expname}')\n", "plt.legend(frameon=True)\n", "plt.tight_layout()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "ExecuteTime": { "end_time": "2019-03-30T03:07:32.899340Z", "start_time": "2019-03-30T03:07:32.707237Z" }, "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" }, { "name": "stdout", "output_type": "stream", "text": [ "2.055529438642871\n", "\n", "array([2.405998, 1.723965, 1.835029, 1.866649, 1.630832, 1.357342, 0.806673,\n", " 1.050754, 1.002628, 2.586103, 3.321667, 2.622398, 1.778602, 1.440388,\n", " 1.22571 , 2.07791 , 2.24657 , 1.221091, 0.760758, 1.038913, 1.049729,\n", " 2.612844, 2.935931, 3.59239 ])\n", "Coordinates:\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24\n", "0.19245008972987526\n", "\n", "array([0.951781, 0.681978, 0.725913, 0.738422, 0.645136, 0.536947, 0.319109,\n", " 0.415664, 0.396626, 1.023028, 1.314008, 1.037386, 0.703591, 0.569798,\n", " 0.484875, 0.821994, 0.888713, 0.483048, 0.300946, 0.41098 , 0.415259,\n", " 1.033607, 1.161415, 1.421102])\n", "Coordinates:\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 ... 16 17 18 19 20 21 22 23 24\n" ] } ], "source": [ "# plot errorbar <=1980\n", "plt.figure(figsize=(7,4))\n", "expname = 'FLOR_nudge'\n", "year0 = 1980\n", "da = flors['pr_a_2d']\n", "da = da.isel(year=da.year<=year0)\n", "lw = 2\n", "alpha = .8\n", "csize = 5\n", "\n", "# c = 'k'\n", "# s = 'Neutral'\n", "# _years = years_neut\n", "# spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "# da_mean = da.sel(year=_years).mean('year')\n", "# da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", "# color=c, alpha=alpha, capsize=csize, label=s\n", "# )\n", "\n", "c = 'C3'\n", "s = 'Nino'\n", "_years = flors['years_nino']\n", "_years = _years[_years<=year0]\n", "spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "da_mean = da.sel(year=_years).mean('year')\n", "da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", " color=c, alpha=alpha, capsize=csize, label=s\n", " )\n", "print(p2t(0.05, df=_years.size-1))\n", "print(da.sel(year=_years).std('year'))\n", "print((_years.size)**(-1/2))\n", "print(spread)\n", "\n", "\n", "c = 'C0'\n", "s = 'Nina'\n", "_years = flors['years_nina']\n", "_years = _years[_years<=year0]\n", "spread = p2t(0.05, df=_years.size-1) * da.sel(year=_years).std('year') * (_years.size)**(-1/2)\n", "da_mean = da.sel(year=_years).mean('year')\n", "da_mean.pipe((plt.errorbar, 'y'), x=da.month, yerr=spread,\n", " color=c, alpha=alpha, capsize=csize, label=s\n", " )\n", "\n", "plt.axhline(0, color='gray', ls='--')\n", "plt.axvline(12.5, color='gray', ls='--')\n", "month_names = month2name(np.mod(da.month.data-1, 12) + 1)\n", "plt.xticks(da.month, month_names)\n", "plt.xlim(6-.5, 6+12+.5)\n", "plt.ylabel('Precip anomaly normalized')\n", "plt.title(f'{expname}, before {year0+1}')\n", "plt.legend(frameon=True)\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## end" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-03-12T20:14:32.030675Z", "start_time": "2019-03-12T20:14:32.027197Z" } }, "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.2" }, "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 }