11# Maps
22
33``` python
4- import os
5- import warnings
6-
7- import numpy as np
8- import pandas as pd
9-
10-
11- os.chdir(" ../../src" )
12-
134import sgis as sg
14-
15-
16- # ignore some warnings
17- pd.options.mode.chained_assignment = None
18- warnings.filterwarnings(action = " ignore" , category = FutureWarning )
195```
206
217First get 1000 addresses in Oslo and create a distance to neighbors column.
@@ -55,8 +41,11 @@ print(points)
5541Create and save a simple plot with legend and title.
5642
5743``` python
58- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors" )
59- m.title = " Distance to 99 nearest neighbors"
44+ m = sg.ThematicMap(
45+ sg.buff(points, 100 ),
46+ column = " mean_dist_99_neighbors" ,
47+ title = " Distance to 99 nearest neighbors" ,
48+ )
6049m.plot()
6150m.save(" path_to_file.png" )
6251```
@@ -66,16 +55,16 @@ m.save("path_to_file.png")
6655Customising the colors and text. Creating an ugly example.
6756
6857``` python
69- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors " )
70-
71- m.title = " Map with custom (and ugly) colors. "
72- m.title_fontsize = 15
73- m.title_color = " red "
74-
75- m.facecolor = " #edf0c9" # background color
76-
77- m.change_cmap( " PuBuGn " , start = 20 , stop = 250 ) # start and stop goes from 0 to 256
78-
58+ m = sg.ThematicMap(
59+ sg.buff(points, 100 ),
60+ column = " mean_dist_99_neighbors " ,
61+ title = " Map with custom (and ugly) colors. " ,
62+ title_fontsize = 15 ,
63+ title_color = " red " ,
64+ facecolor = " #edf0c9" , # background color
65+ cmap = " PuBuGn " ,
66+ cmap_start = 20 ,
67+ )
7968m.plot()
8069```
8170
@@ -86,16 +75,19 @@ See here for available cmaps: https://matplotlib.org/stable/tutorials/colors/col
8675Customising the legend can be done through the legend attribute.
8776
8877``` python
89- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors" )
90-
91- m.title = " Map with customised legend"
92-
93- m.legend.title = " Meters"
94- m.legend.label_sep = " to"
95- m.legend.label_suffix = " m"
96- m.legend.position = (0.35 , 0.28 )
97- m.legend.rounding = 1
98-
78+ m = sg.ThematicMap(
79+ sg.buff(points, 100 ),
80+ column = " mean_dist_99_neighbors" ,
81+ title = " Map with customised legend" ,
82+ legend_kwargs = dict (
83+ title = " Meters" ,
84+ label_sep = " to" ,
85+ label_suffix = " m" ,
86+ position = (0.35 , 0.28 ),
87+ rounding = 1 ,
88+ pretty_labels = True ,
89+ ),
90+ )
9991m.plot()
10092```
10193
@@ -104,23 +96,21 @@ m.plot()
10496Using custom breaks and labels for the color classification.
10597
10698``` python
107- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors" )
108-
109- m.title = " Map with custom bins and legend labels"
110- m.legend.title = " Custom labels"
111-
112- m.bins = [1500 , 2000 , 2500 , 3000 ]
113-
114- min_value = points.mean_dist_99_neighbors.min()
115- max_value = points.mean_dist_99_neighbors.max()
99+ m = sg.ThematicMap(
100+ sg.buff(points, 100 ),
101+ column = " mean_dist_99_neighbors" ,
102+ title = " Map with custom bins" ,
103+ bins = [1500 , 2000 , 2500 , 3000 ],
104+ )
105+ min_value = points[" mean_dist_99_neighbors" ].min()
106+ max_value = points[" mean_dist_99_neighbors" ].max()
116107m.legend.labels = [
117108 f " { int (round (min_value))} to 1499 " ,
118109 " 1500 to 1999" ,
119110 " 2000 to 2499" ,
120111 " 2500 to 2999" ,
121112 f " 3000 to { int (round (max_value))} " ,
122113]
123-
124114m.plot()
125115```
126116
@@ -131,10 +121,15 @@ maximum values in each color group. This will be accurate and truthful, but
131121somewhat confusing.
132122
133123``` python
134- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors" )
135- m.title = " Map with custom bins, but default legend labels"
136- m.bins = [1500 , 2000 , 2500 , 3000 ]
137- m.legend.title = " Default legend labels"
124+ m = sg.ThematicMap(
125+ sg.buff(points, 100 ),
126+ column = " mean_dist_99_neighbors" ,
127+ title = " Map with custom bins, but default legend labels" ,
128+ bins = [1500 , 2000 , 2500 , 3000 ],
129+ legend_kwargs = {
130+ " title" : " Default legend labels" ,
131+ },
132+ )
138133m.plot()
139134```
140135
@@ -145,14 +140,16 @@ The background gdf will be gray by default, but can be changed by setting
145140the color.
146141
147142``` python
148- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors" )
149-
150- m.title = " Map with a background GeoDataFrame"
151- m.legend.title = " Meters"
152-
143+ m = sg.ThematicMap(
144+ sg.buff(points, 100 ),
145+ column = " mean_dist_99_neighbors" ,
146+ title = " Map with a background GeoDataFrame" ,
147+ legend_kwargs = {
148+ " title" : " Meters" ,
149+ },
150+ )
153151background = sg.buff(points, 500 )
154152m.add_background(background, color = None )
155-
156153m.plot()
157154```
158155
@@ -162,14 +159,17 @@ Setting black to True gives opposite colors and a palette suited for a black
162159background (viridis).
163160
164161``` python
165- m = sg.ThematicMap(points, column = " mean_dist_99_neighbors" , black = True )
166-
167- m.title = " black=True, with background GeoDataFrame"
168- m.legend.title = " Meters"
169-
162+ m = sg.ThematicMap(
163+ points,
164+ column = " mean_dist_99_neighbors" ,
165+ black = True ,
166+ title = " black=True, with background GeoDataFrame" ,
167+ legend_kwargs = {
168+ " title" : " Meters" ,
169+ },
170+ )
170171background = sg.buff(points, 500 )
171172m.add_background(background)
172-
173173m.plot()
174174```
175175
@@ -178,33 +178,34 @@ m.plot()
178178Customising all at once.
179179
180180``` python
181- m = sg.ThematicMap(sg.buff(points, 100 ), column = " mean_dist_99_neighbors" )
182-
183- m.title = " Everything customised"
184-
185- m.facecolor = " #fcfcfc" # =almost white
186- m.change_cmap(" YlGnBu" , start = 50 , stop = 256 ) # start and stop goes from 0 to 256
187-
188- m.title_fontsize = 125
189- m.title_color = " #141414" # =almost black
190-
191- m.legend.title = " Meters"
192- m.legend.title_fontsize = 55
193- m.legend.fontsize = 45
194- m.legend.markersize = 30
195- m.legend.position = (0.35 , 0.28 )
196-
197- m.bins = [1500 , 2000 , 2500 , 3000 ]
198-
199- min_value = points.mean_dist_99_neighbors.min()
200- max_value = points.mean_dist_99_neighbors.max()
201- m.legend.labels = [
202- f " { int (round (min_value))} to 1499 " ,
203- " 1500 to 1999" ,
204- " 2000 to 2499" ,
205- " 2500 to 2999" ,
206- f " 3000 to { int (round (max_value))} " ,
207- ]
181+ min_value = points[" mean_dist_99_neighbors" ].min()
182+ max_value = points[" mean_dist_99_neighbors" ].max()
183+
184+ m = sg.ThematicMap(
185+ sg.buff(points, 100 ),
186+ column = " mean_dist_99_neighbors" ,
187+ title = " Everything customised" ,
188+ bins = [1500 , 2000 , 2500 , 3000 ],
189+ facecolor = " #fcfcfc" , # =almost white
190+ cmap = " YlGnBu" ,
191+ cmap_start = 50 ,
192+ title_fontsize = 125 ,
193+ title_color = " #141414" , # =almost black
194+ legend_kwargs = dict (
195+ title = " Meters" ,
196+ title_fontsize = 55 ,
197+ fontsize = 45 ,
198+ markersize = 30 ,
199+ position = (0.35 , 0.28 ),
200+ labels = [
201+ f " { int (round (min_value))} to 1499 " ,
202+ " 1500 to 1999" ,
203+ " 2000 to 2499" ,
204+ " 2500 to 2999" ,
205+ f " 3000 to { int (round (max_value))} " ,
206+ ],
207+ ),
208+ )
208209
209210background = sg.buff(points, 500 )
210211m.add_background(background, color = " #f0f0f0" )
0 commit comments