{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Caution, basemap is at end-of-life. You should use cartopy for plotting maps with projections\n", "\n", "### WeatherGod commented on Aug 9, 2017\n", "I declared the EoL for Basemap last year on the webpage: https://matplotlib.org/basemap/users/intro.html#cartopy-new-management-and-eol-announcement\n", "\n", "I encourage everyone to move to Cartopy because it is a superior API design. I have also advertised this for the past two SciPy conferences. Basemap just has no hope of being maintained.\n", "\n", "I should point out that you will soon be getting some competition from the GMT project as they move into the python space, but they don't integrate at all with matplotlib, and there isn't any hope for interactive plotting at the moment. But, their figures are very pretty...\n", "\n", "@WeatherGod (Benjamin Root)\n", "\n", "\n", "### WeatherGod commented on Aug 9, 2017\n", "Side note: I am the sole remaining \"Basemap folks\". I am just making sure the lights are turned off before I leave.\n", "\n", "@WeatherGod (Benjamin Root)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "import os\n", "import matplotlib.pyplot as plt\n", "from netCDF4 import Dataset as netcdf_dataset\n", "import numpy as np\n", "import xarray as xr\n", "\n", "from cartopy import config\n", "import cartopy.crs as ccrs\n", "import matplotlib.ticker as mticker\n", "from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n", "from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter\n", "import cartopy.feature as cfeature \n", "# When data are defined in lat/lon coordinate system, PlateCarree()\n", "# is the appropriate choice:\n", "\n", "from cartopy.util import add_cyclic_point" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More plotting help\n", "\n", "* [matplotlib gallery:](https://matplotlib.org/gallery.html#misc)\n", "* [colormaps:](https://matplotlib.org/examples/color/colormaps_reference.html)\n", "* [cartopy:](http://scitools.org.uk/cartopy/docs/v0.14/index.html)\n", "* [geoviews:](http://geo.holoviews.org/Gridded_Datasets_I.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get the path of the file. It can be found in the repo data directory.\n", "fname = os.path.join(config[\"repo_data_dir\"],\n", " 'netcdf', 'HadISST1_SST_update.nc'\n", " )\n", "\n", "dataset = netcdf_dataset(fname)\n", "sst = dataset.variables['sst'][0, :, :]\n", "lats = dataset.variables['lat'][:]\n", "lons = dataset.variables['lon'][:]\n", "data_crs = ccrs.PlateCarree() # since our data is on a rectangular lon,lat grid\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "ax = plt.axes(projection=ccrs.PlateCarree())\n", "plt.contourf(lons, lats, sst, 60,\n", " transform=ccrs.PlateCarree(), cmap = \"jet\")\n", "gl = ax.gridlines(crs=data_crs, draw_labels=True,\n", " linewidth=1, color='gray', alpha=0.5, linestyle='--')\n", "gl.xlabels_top = False\n", "gl.ylabels_right = False\n", "gl.xlines = False\n", "\n", "ax.coastlines()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "ax2 = plt.axes(projection=ccrs.Robinson())\n", "plt.contourf(lons, lats, sst, 60, transform=data_crs, cmap = \"jet\")\n", "ax2.coastlines()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A rotated pole projection again...\n", "projection = ccrs.RotatedPole(pole_longitude=-177.5, pole_latitude=37.5)\n", "ax = plt.axes(projection=projection)\n", "ax.set_global()\n", "ax.coastlines()\n", "\n", "# ...but now using the transform argument\n", "sst_cyc, lons_cyc = add_cyclic_point(sst, coord=lons)\n", "ax.contourf(lons_cyc, lats, sst_cyc, 60, transform=data_crs, cmap = \"jet\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "airtemps = xr.tutorial.load_dataset('air_temperature')\n", "sst_cyc, lons_cyc = add_cyclic_point(sst, coord=lons)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(20, 12))\n", "ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))\n", "im = ax.contourf(lons_cyc, lats, sst_cyc, 60, vmin = -2, vmax = 32, transform=data_crs, cmap = \"gist_rainbow_r\")\n", "cb = plt.colorbar(im, orientation='vertical', shrink = 0.68, label=r'$\\degree C$')\n", "#ax.stock_img()\n", "ax.set_xticks([-120, -60 ,0, 60, 120], crs=ccrs.PlateCarree())\n", "ax.set_yticks([-80,-40, 0, 40, 80], crs=ccrs.PlateCarree())\n", "lon_formatter = LongitudeFormatter(zero_direction_label=True)\n", "lat_formatter = LatitudeFormatter()\n", "ax.xaxis.set_major_formatter(lon_formatter)\n", "ax.yaxis.set_major_formatter(lat_formatter)\n", "ax.gridlines()\n", "ax.add_feature(cfeature.OCEAN, alpha=0.3)\n", "ax.add_feature(cfeature.COASTLINE,linewidth=2)\n", "ax.add_feature(cfeature.BORDERS, linestyle=':')\n", "ax.add_feature(cfeature.LAKES, alpha=1.0)\n", "ax.add_feature(cfeature.RIVERS)\n", "plt.title(r'Our map of $T_s$ ($\\degree C$)', fontsize=20)\n", "plt.savefig('WorldMap.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Py3 pangeo", "language": "python", "name": "pangeo" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }