Skip to content

Commit

Permalink
Merge pull request NCAR#145 from jukent/julyexamples2
Browse files Browse the repository at this point in the history
Julyexamples2
  • Loading branch information
jukent authored Jul 27, 2023
2 parents 72c0f95 + ff17710 commit ef93ccc
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 7 deletions.
Binary file added docs/_static/thumbnails/find_local_extrema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/thumbnails/plot_contour_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/thumbnails/plot_extrema_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ Here are some examples of how to use geocat-viz.
examples/set_tick_direction_spine_visibility.ipynb
examples/add_right_hand_axis.ipynb
examples/add_major_minor_ticks.ipynb
examples/find_local_extrema.ipynb
examples/plot_contour_labels.ipynb
examples/plot_extrema_labels.ipynb
98 changes: 98 additions & 0 deletions docs/examples/find_local_extrema.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# `find_local_extrema`\n",
"\n",
"This notebook is a simple example of the GeoCAT-viz function <a href=\"../user_api/generated/geocat.viz.util.find_local_extrema.html#geocat-viz.util.find_local_extrema\">find_local_extrema</a>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import xarray as xr\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import geocat.viz as gv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Generate dummy data\n",
"data = [[1, 4, 5, 6, 8.2],\n",
" [9, 8.4, 10, 10.6, 9.7],\n",
" [4.4, 5, 0, 6.6, 1.4],\n",
" [4.6, 5.2, 1.5, 7.6, 2.4]]\n",
"\n",
"# Convert data into type xarray.DataArray\n",
"data = xr.DataArray(data,\n",
" dims=[\"lat\", \"lon\"],\n",
" coords=dict(lat=np.arange(4), lon=np.arange(5)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Plot:\n",
"\n",
"# Generate figure (set its size (width, height) in inches)\n",
"plt.figure(figsize=(9.5, 8))\n",
"\n",
"# Generate axes\n",
"ax = plt.axes()\n",
"\n",
"# Plot filled contour and contour lines\n",
"contours = ax.contourf(data)\n",
"\n",
"# Find local min/max extrema with GeoCAT-Viz find_local_extrema\n",
"lmin = gv.find_local_extrema(data, eType='Low')[0]\n",
"lmax = gv.find_local_extrema(data, eType='High')[0]\n",
"\n",
"# Plot labels for local extrema\n",
"min_value = data.data[lmin[1]][lmin[0]]\n",
"ax.text(lmin[0], lmin[1], str(min_value))\n",
"\n",
"max_value = data.data[lmax[1]][lmax[0]]\n",
"ax.text(lmax[0], lmax[1], str(max_value))\n",
"\n",
"# Show plot\n",
"plt.show();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geocat_viz_build",
"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.10.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
125 changes: 125 additions & 0 deletions docs/examples/plot_contour_labels.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# `plot_contour_labels`\n",
"\n",
"This notebook is a simple example of the GeoCAT-viz function <a href=\"../user_api/generated/geocat.viz.util.plot_contour_labels.html#geocat-viz.util.plot_contour_labels\">plot_contour_labels</a>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import packages:\n",
"\n",
"import xarray as xr\n",
"import matplotlib.pyplot as plt\n",
"import cartopy.crs as ccrs\n",
"import cartopy.feature as cfeature\n",
"import numpy as np\n",
"\n",
"import geocat.datafiles as gdf\n",
"import geocat.viz as gv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read in data:\n",
"\n",
"# Open a netCDF data file using xarray default engine and\n",
"# load the data into xarrays\n",
"ds = xr.open_dataset(gdf.get(\"netcdf_files/slp.1963.nc\"), decode_times=False)\n",
"\n",
"# Get data from the 24th timestep\n",
"pressure = ds.slp[24, :, :]\n",
"\n",
"# Translate short values to float values\n",
"pressure = pressure.astype('float64')\n",
"\n",
"# Convert Pa to hPa data\n",
"pressure = pressure * 0.01\n",
"\n",
"# Fix the artifact of not-shown-data around 0 and 360-degree longitudes\n",
"wrap_pressure = gv.xr_add_cyclic_longitudes(pressure, \"lon\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create plot\n",
"\n",
"# Set figure size\n",
"fig = plt.figure(figsize=(8, 8))\n",
"\n",
"# Set global axes with an orthographic projection\n",
"proj = ccrs.Orthographic(central_longitude=270, central_latitude=45)\n",
"ax = plt.axes(projection=proj)\n",
"ax.set_global()\n",
"\n",
"# Plot contour data\n",
"p = wrap_pressure.plot.contour(ax=ax,\n",
" transform=ccrs.PlateCarree(),\n",
" linewidths=0.5,\n",
" cmap='black',\n",
" add_labels=False)\n",
"\n",
"contour_label_locations = [(176.4, 34.63), (-150.46, 42.44), (-142.16, 28.5),\n",
" (-134.12, 16.32), (-108.9, 17.08), (-98.17, 15.6),\n",
" (-108.73, 42.19), (-111.25, 49.66), (-127.83, 41.93),\n",
" (-92.49, 25.64), (-77.29, 29.08), (-77.04, 16.42),\n",
" (-95.93, 57.59), (-156.05, 84.47), (-17.83, 82.52),\n",
" (-76.3, 41.99), (-48.89, 41.45), (-33.43, 37.55),\n",
" (-46.98, 17.17), (1.79, 63.67), (-58.78, 67.05),\n",
" (-44.78, 53.68), (-69.69, 53.71), (-78.02, 52.22),\n",
" (-16.91, 44.33), (-95.72, 35.17), (-102.69, 73.62)]\n",
"\n",
"# Plot Clabels\n",
"gv.plot_contour_labels(ax,\n",
" p,\n",
" ccrs.Geodetic(),\n",
" proj,\n",
" clabel_locations=contour_label_locations)\n",
"\n",
"\n",
"# Make layout tight\n",
"plt.tight_layout()\n",
"\n",
"plt.show();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geocat_viz_build",
"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.10.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
127 changes: 127 additions & 0 deletions docs/examples/plot_extrema_labels.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# `plot_extrema_labels`\n",
"\n",
"This notebook is a simple example of the GeoCAT-viz function <a href=\"../user_api/generated/geocat.viz.util.plot_extrema_labels.html#geocat-viz.util.plot_extrema_labels\">plot_extrema_labels</a>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import packages:\n",
"\n",
"import xarray as xr\n",
"import cartopy.crs as ccrs\n",
"import cartopy.feature as cfeature\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib import colors\n",
"import matplotlib.ticker as mticker\n",
"\n",
"import geocat.datafiles as gdf\n",
"import geocat.viz as gv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read in data:\n",
"\n",
"# Open a netCDF data file using xarray default engine and\n",
"# load the data into xarrays\n",
"ds = xr.open_dataset(gdf.get(\"netcdf_files/slp.1963.nc\"), decode_times=False)\n",
"\n",
"# Get data from the 21st timestep\n",
"pressure = ds.slp[21, :, :]\n",
"\n",
"# Translate float values to short values\n",
"pressure = pressure.astype('float32')\n",
"\n",
"# Convert Pa to hPa data\n",
"pressure = pressure * 0.01\n",
"\n",
"# Fix the artifact of not-shown-data around 0 and 360-degree longitudes\n",
"wrap_pressure = gv.xr_add_cyclic_longitudes(pressure, \"lon\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create plot\n",
"\n",
"# Set figure size\n",
"fig = plt.figure(figsize=(8, 8))\n",
"\n",
"# Set global axes with an orthographic projection\n",
"proj = ccrs.Orthographic(central_longitude=270, central_latitude=45)\n",
"ax = plt.axes(projection=proj)\n",
"ax.set_global()\n",
"\n",
"# Plot contour data\n",
"p = wrap_pressure.plot.contour(ax=ax,\n",
" transform=ccrs.PlateCarree(),\n",
" linewidths=0.3,\n",
" levels=30,\n",
" cmap='black',\n",
" add_labels=False)\n",
"\n",
"# low pressure contour levels- these will be plotted\n",
"# as a subscript to an 'L' symbol.\n",
"lowClevels = gv.find_local_extrema(pressure, lowVal=995, eType='Low')\n",
"highClevels = gv.find_local_extrema(pressure, highVal=1042, eType='High')\n",
"\n",
"# Label low and high contours\n",
"gv.plot_extrema_labels(wrap_pressure,\n",
" ccrs.Geodetic(),\n",
" proj,\n",
" label_locations=lowClevels,\n",
" label='L',\n",
" show_warnings=False)\n",
"gv.plot_extrema_labels(wrap_pressure,\n",
" ccrs.Geodetic(),\n",
" proj,\n",
" label_locations=highClevels,\n",
" label='H',\n",
" show_warnings=False)\n",
"\n",
"\n",
"plt.show();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geocat_viz_build",
"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.10.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
12 changes: 12 additions & 0 deletions docs/gallery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@
- title: 'add_major_minor_ticks'
path: examples/add_major_minor_ticks.ipynb
thumbnail: _static/thumbnails/add_major_minor_ticks.png

- title: 'find_local_extrema'
path: examples/find_local_extrema.ipynb
thumbnail: _static/thumbnails/find_local_extrema.png

- title: 'plot_contour_labels'
path: examples/plot_contour_labels.ipynb
thumbnail: _static/thumbnails/plot_contour_labels.png

- title: 'plot_extrema_labels'
path: examples/plot_extrema_labels.ipynb
thumbnail: _static/thumbnails/plot_extrema_labels.png
Loading

0 comments on commit ef93ccc

Please sign in to comment.