Coverage for src/pyensae/graphhelper/mapplot.py: 18%

11 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-03 02:16 +0200

1""" 

2@file 

3@brief Plotting maps. 

4""" 

5 

6 

7def plot_map_france(ax=None, scale='50m'): 

8 """ 

9 Creates a map for France using :epkg:`cartopy`. 

10 

11 @param ax existing axes or None to create ones 

12 @param scale scale in (`10m`, `50m`, `110m`) 

13 @return ax 

14 

15 .. plot:: 

16 

17 from matplotlib import pyplot as plt 

18 from pyensae.graphhelper import plot_map_france 

19 

20 plot_map_france() 

21 plt.show() 

22 

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 """ 

27 

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]) 

35 

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 

41 

42 

43def plot_map_france_polygon(geometry, colors, ax=None, scale='50m'): 

44 """ 

45 Plots polygons into a map for France. 

46 

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 

52 

53 .. plot:: 

54 

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 

59 

60 # loads the French departments 

61 df = load_french_departements() 

62 

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])]) 

69 

70 plt.show() 

71 

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