Coverage for pyguymer3/geo/_add_vertical_gridlines.py: 6%
18 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def _add_vertical_gridlines(
5 ax,
6 /,
7 *,
8 color = "black",
9 linestyle = ":",
10 linewidth = 0.5,
11 locs = None,
12 ngrid = -1,
13 npoint = 181,
14 zorder = 2.0,
15):
16 """Add vertical gridlines to a Cartopy axis.
18 Parameters
19 ----------
20 ax : cartopy.mpl.geoaxes.GeoAxesSubplot
21 the axis to add the gridlines to
22 color : str, optional
23 the colour of the gridlines
24 linestyle : str, optional
25 the style of the gridlines
26 linewidth : float, optional
27 the width of the gridlines
28 locs : list of float, optional
29 the locations of the gridlines (in degrees)
30 ngrid : int, optional
31 the number of gridlines to draw
32 npoint : int, optional
33 the number of points along each gridline to draw
34 zorder : float, optional
35 the zorder to draw the gridlines with (the default value has been chosen
36 to match the value that it ends up being if the gridlines are not drawn
37 with the zorder keyword specified -- obtained by manual inspection on
38 5/Dec/2023)
40 Notes
41 -----
42 If ``ngrid`` is more than "1" then it is used to create the gridline
43 locations. If it is not, then the gridline locations will attempt to be made
44 from ``locs`` instead.
46 Copyright 2017 Thomas Guymer [1]_
48 References
49 ----------
50 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
51 """
53 # Import standard modules ...
54 import os
56 # Import special modules ...
57 try:
58 import cartopy
59 cartopy.config.update(
60 {
61 "cache_dir" : os.path.expanduser("~/.local/share/cartopy_cache"),
62 }
63 )
64 except:
65 raise Exception("\"cartopy\" is not installed; run \"pip install --user Cartopy\"") from None
66 try:
67 import numpy
68 except:
69 raise Exception("\"numpy\" is not installed; run \"pip install --user numpy\"") from None
71 # **************************************************************************
73 # Determine x-locations depending on inputs ...
74 if ngrid > 1:
75 xlocs = numpy.linspace(-180.0, +180.0, num = ngrid) # [°]
76 elif locs is not None:
77 xlocs = numpy.array(locs) # [°]
78 else:
79 raise Exception("\"ngrid > 1\" is not True and \"locs is not None\" is not True") from None
81 # Loop over x-locations ...
82 for xloc in xlocs:
83 # Add gridline to axis ...
84 ax.plot(
85 xloc * numpy.ones(npoint),
86 numpy.linspace(-90.0, +90.0, num = npoint),
87 color = color,
88 linestyle = linestyle,
89 linewidth = linewidth,
90 transform = cartopy.crs.PlateCarree(),
91 zorder = zorder,
92 )