Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Plotting maps.
4"""
7def plot_map_france(ax=None, scale='50m'):
8 """
9 Creates a map for France using :epkg:`cartopy`.
11 @param ax existing axes or None to create ones
12 @param scale scale in (`10m`, `50m`, `110m`)
13 @return ax
15 .. plot::
17 from matplotlib import pyplot as plt
18 from pyensae.graphhelper import plot_map_france
20 plot_map_france()
21 plt.show()
23 You may want to add `ax.set_extent([-5., 10., 38., 52.])`
24 after `ax = plot_map_france()`.
25 See also example :ref:`l-map-france`.
26 """
28 if ax is None: # pragma: no cover
29 import matplotlib.pyplot as plt # pylint: disable=C0415
30 import cartopy.crs as ccrs # pylint: disable=C0415
31 fig = plt.figure(figsize=(8, 8))
32 ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
33 # The following line may make the program crash on debian + python3.9.1.
34 # ax.set_extent([-5, 10, 38, 52])
36 import cartopy.feature as cfeature # pylint: disable=C0415
37 ax.add_feature(cfeature.OCEAN.with_scale(scale))
38 ax.add_feature(cfeature.RIVERS.with_scale(scale))
39 ax.add_feature(cfeature.BORDERS.with_scale(scale), linestyle=':')
40 return ax
43def plot_map_france_polygon(geometry, colors, ax=None, scale='50m'):
44 """
45 Plots polygons into a map for France.
47 @param geometry series of polygons
48 @param colors colors
49 @param scale scale, see @see fn map_france
50 @param ax existing axes, None to create one
51 @return ax
53 .. plot::
55 from matplotlib import pyplot as plt
56 import cartopy.crs as ccrs
57 from pyensae.datasource import load_french_departements
58 from pyensae.graphhelper import plot_map_france, plot_map_france_polygon
60 # loads the French departments
61 df = load_french_departements()
63 fig = plt.figure(figsize=(7,7))
64 ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
65 N = float(df.shape[0])
66 plot_map_france_polygon(
67 ax=ax, geometry=df['geometry'],
68 colors=[(i/N, i/N, i/N) for i in range(df.shape[0])])
70 plt.show()
72 You may want to add `ax.set_extent([-5., 10., 38., 52.])`
73 after `ax = plot_map_france_polygon()`.
74 See also example :ref:`l-map-france`.
75 """
76 from geopandas.plotting import plot_polygon_collection # pylint: disable=C0415
77 ax = plot_map_france(scale=scale, ax=ax)
78 plot_polygon_collection(
79 ax, geometry, facecolor=colors, values=None, edgecolor='black')
80 return ax