OSGeoLive-Notebooks/Cartopy/cartopy-quiver-regrid.ipynb

127 lines
3.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"\n",
"import cartopy.crs as ccrs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Regridding vectors with quiver\n",
"------------------------------\n",
"\n",
"This example demonstrates the regridding functionality in quiver (there exists\n",
"equivalent functionality in :meth:`cartopy.mpl.geoaxes.GeoAxes.barbs`).\n",
"\n",
"Regridding can be an effective way of visualising a vector field, particularly\n",
"if the data is dense or warped.\n",
"\n",
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sample_data(shape=(20, 30)):\n",
" \"\"\"\n",
" Returns ``(x, y, u, v, crs)`` of some vector data\n",
" computed mathematically. The returned CRS will be a North Polar\n",
" Stereographic projection, meaning that the vectors will be unevenly\n",
" spaced in a PlateCarree projection.\n",
"\n",
" \"\"\"\n",
" crs = ccrs.NorthPolarStereo()\n",
" scale = 1e7\n",
" x = np.linspace(-scale, scale, shape[1])\n",
" y = np.linspace(-scale, scale, shape[0])\n",
"\n",
" x2d, y2d = np.meshgrid(x, y)\n",
" u = 10 * np.cos(2 * x2d / scale + 3 * y2d / scale)\n",
" v = 20 * np.cos(6 * x2d / scale)\n",
"\n",
" return x, y, u, v, crs\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def main():\n",
" plt.figure(figsize=(8, 10))\n",
"\n",
" x, y, u, v, vector_crs = sample_data(shape=(50, 50))\n",
" ax1 = plt.subplot(2, 1, 1, projection=ccrs.PlateCarree())\n",
" ax1.coastlines()\n",
" ax1.set_extent([-45, 55, 20, 80], ccrs.PlateCarree())\n",
" ax1.quiver(x, y, u, v, transform=vector_crs)\n",
"\n",
" ax2 = plt.subplot(2, 1, 2, projection=ccrs.PlateCarree())\n",
" plt.title('The same vector field regridded')\n",
" ax2.coastlines()\n",
" ax2.set_extent([-45, 55, 20, 80], ccrs.PlateCarree())\n",
" ax2.quiver(x, y, u, v, transform=vector_crs, regrid_shape=20)\n",
"\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}