Skip to content

Commit

Permalink
Gallery: simplify quiver example (#4120)
Browse files Browse the repository at this point in the history
* simplify quiver example

* whatsnew; reinstate 2nd call to figure()

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bill Little <bill.james.little@gmail.com>
  • Loading branch information
3 people committed Sep 15, 2021
1 parent 121df05 commit 5d44601
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
51 changes: 15 additions & 36 deletions docs/gallery_code/meteorology/plot_wind_speed.py
Expand Up @@ -11,71 +11,50 @@
"""

import cartopy.crs as ccrs
import cartopy.feature as cfeat
import matplotlib.pyplot as plt
import numpy as np

import iris
import iris.coord_categorisation
import iris.plot as iplt
import iris.quickplot as qplt


def main():
# Load the u and v components of wind from a pp file
# Load the u and v components of wind from a pp file.
infile = iris.sample_data_path("wind_speed_lake_victoria.pp")

uwind = iris.load_cube(infile, "x_wind")
vwind = iris.load_cube(infile, "y_wind")

ulon = uwind.coord("longitude")
vlon = vwind.coord("longitude")

# The longitude points go from 180 to 540, so subtract 360 from them
ulon.points = ulon.points - 360.0
vlon.points = vlon.points - 360.0

# Create a cube containing the wind speed
# Create a cube containing the wind speed.
windspeed = (uwind ** 2 + vwind ** 2) ** 0.5
windspeed.rename("windspeed")

x = ulon.points
y = uwind.coord("latitude").points
u = uwind.data
v = vwind.data
# Plot the wind speed as a contour plot.
qplt.contourf(windspeed, 20)

# Set up axes to show the lake
# Show the lake on the current axes.
lakes = cfeat.NaturalEarthFeature(
"physical", "lakes", "50m", facecolor="none"
)
plt.gca().add_feature(lakes)

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(lakes)

# Get the coordinate reference system used by the data
transform = ulon.coord_system.as_cartopy_projection()

# Plot the wind speed as a contour plot
qplt.contourf(windspeed, 20)

# Add arrows to show the wind vectors
plt.quiver(x, y, u, v, pivot="middle", transform=transform)
# Add arrows to show the wind vectors.
iplt.quiver(uwind, vwind, pivot="middle")

plt.title("Wind speed over Lake Victoria")
qplt.show()

# Normalise the data for uniform arrow size
u_norm = u / np.sqrt(u ** 2.0 + v ** 2.0)
v_norm = v / np.sqrt(u ** 2.0 + v ** 2.0)
# Normalise the data for uniform arrow size.
u_norm = uwind / windspeed
v_norm = vwind / windspeed

# Make a new figure for the normalised plot.
plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(lakes)

qplt.contourf(windspeed, 20)

plt.quiver(x, y, u_norm, v_norm, pivot="middle", transform=transform)
plt.gca().add_feature(lakes)
iplt.quiver(u_norm, v_norm, pivot="middle")

plt.title("Wind speed over Lake Victoria")
qplt.show()
Expand Down
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Expand Up @@ -68,6 +68,9 @@ This document explains the changes made to Iris for this release
📚 Documentation
================

#. `@rcomer`_ updated the "Plotting Wind Direction Using Quiver" Gallery
example. (:pull:`4120`)

#. `@trexfeathers`_ included `Iris GitHub Discussions`_ in
:ref:`get involved <development_where_to_start>`. (:pull:`4307`)

Expand Down

0 comments on commit 5d44601

Please sign in to comment.