matplotlib-map-utils 1.0.3__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- matplotlib_map_utils-2.0.1/PKG-INFO +285 -0
- matplotlib_map_utils-2.0.1/README.md +268 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/__init__.py +6 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/core/__init__.py +4 -0
- {matplotlib_map_utils-1.0.3/matplotlib_map_utils → matplotlib_map_utils-2.0.1/matplotlib_map_utils/core}/north_arrow.py +56 -55
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/core/scale_bar.py +1220 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/defaults/__init__.py +4 -0
- matplotlib_map_utils-1.0.3/matplotlib_map_utils/defaults.py → matplotlib_map_utils-2.0.1/matplotlib_map_utils/defaults/north_arrow.py +4 -4
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/defaults/scale_bar.py +377 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/validation/__init__.py +2 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/validation/functions.py +231 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/validation/north_arrow.py +175 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils/validation/scale_bar.py +274 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils.egg-info/PKG-INFO +285 -0
- matplotlib_map_utils-2.0.1/matplotlib_map_utils.egg-info/SOURCES.txt +21 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/matplotlib_map_utils.egg-info/requires.txt +1 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/matplotlib_map_utils.egg-info/top_level.txt +2 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/pyproject.toml +6 -2
- matplotlib_map_utils-1.0.3/PKG-INFO +0 -131
- matplotlib_map_utils-1.0.3/README.md +0 -115
- matplotlib_map_utils-1.0.3/matplotlib_map_utils/__init__.py +0 -2
- matplotlib_map_utils-1.0.3/matplotlib_map_utils/validation.py +0 -332
- matplotlib_map_utils-1.0.3/matplotlib_map_utils.egg-info/PKG-INFO +0 -131
- matplotlib_map_utils-1.0.3/matplotlib_map_utils.egg-info/SOURCES.txt +0 -14
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/LICENSE +0 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/matplotlib_map_utils/scratch/map_utils.py +0 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/matplotlib_map_utils/scratch/north_arrow_old_classes.py +0 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/matplotlib_map_utils.egg-info/dependency_links.txt +0 -0
- {matplotlib_map_utils-1.0.3 → matplotlib_map_utils-2.0.1}/setup.cfg +0 -0
@@ -0,0 +1,285 @@
|
|
1
|
+
Metadata-Version: 2.2
|
2
|
+
Name: matplotlib-map-utils
|
3
|
+
Version: 2.0.1
|
4
|
+
Summary: A suite of tools for creating maps in matplotlib
|
5
|
+
Author-email: David Moss <davidmoss1221@gmail.com>
|
6
|
+
Project-URL: Homepage, https://github.com/moss-xyz/matplotlib-map-utils/
|
7
|
+
Project-URL: Bug Tracker, https://github.com/moss-xyz/matplotlib-map-utils/issues
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
|
10
|
+
Classifier: Operating System :: OS Independent
|
11
|
+
Requires-Python: >=3.9
|
12
|
+
Description-Content-Type: text/markdown
|
13
|
+
License-File: LICENSE
|
14
|
+
Requires-Dist: matplotlib>=3.9.0
|
15
|
+
Requires-Dist: cartopy>=0.23.0
|
16
|
+
Requires-Dist: great-circle-calculator>=1.3.1
|
17
|
+
|
18
|
+
# matplotlib-map-utils
|
19
|
+
|
20
|
+
---
|
21
|
+
|
22
|
+
**Documentation**: See `docs` folder
|
23
|
+
|
24
|
+
**Source Code**: [Available on GitHub](https://github.com/moss-xyz/matplotlib-map-utils)
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
### Introduction
|
29
|
+
|
30
|
+
`matplotlib_map_utils` is intended to be a package that provides various functions and objects that assist with the the creation of maps using [`matplotlib`](https://matplotlib.org/stable/).
|
31
|
+
|
32
|
+
As of `v2.x` (the current version), this includes two tools:
|
33
|
+
|
34
|
+
* `north_arrow.py`, which generates a high quality, context-aware north arrow for a given plot.
|
35
|
+
|
36
|
+
* `scale_bar.py`, which generates a high quality, context-aware scale bar to a given plot.
|
37
|
+
|
38
|
+
Future releases (if the project is continued) might provide a similar tool inset maps, or other functions that I have created myself that give more control in the formatting of maps.
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
### Installation
|
43
|
+
|
44
|
+
This package is available on PyPi, and can be installed like so:
|
45
|
+
|
46
|
+
```bash
|
47
|
+
pip install matplotlib-map-utils
|
48
|
+
```
|
49
|
+
|
50
|
+
The requirements for this package are:
|
51
|
+
|
52
|
+
* `python >= 3.9.0` (due to the dictionary-concatenation method utilized)
|
53
|
+
|
54
|
+
* `matplotlib >= 3.9.0` (might work with lower versions but not guaranteed)
|
55
|
+
|
56
|
+
* `cartopy >= 0.23.0` (due to earlier bug with calling `copy()` on `CRS` objects)
|
57
|
+
|
58
|
+
---
|
59
|
+
|
60
|
+
### Package Structure
|
61
|
+
|
62
|
+
The package is arrayed in the following way:
|
63
|
+
|
64
|
+
```bash
|
65
|
+
package_name/
|
66
|
+
├── __init__.py
|
67
|
+
│
|
68
|
+
├── core/
|
69
|
+
│ ├── __init__.py
|
70
|
+
│ ├── north_arrow.py
|
71
|
+
│ ├── scale_bar.py
|
72
|
+
├── validation/
|
73
|
+
│ ├── __init__.py
|
74
|
+
│ ├── functions.py
|
75
|
+
│ ├── north_arrow.py
|
76
|
+
│ └── scale_bar.py
|
77
|
+
├── defaults/
|
78
|
+
│ ├── __init__.py
|
79
|
+
│ ├── north_arrow.py
|
80
|
+
│ └── scale_bar.py
|
81
|
+
```
|
82
|
+
|
83
|
+
Where:
|
84
|
+
|
85
|
+
* `core` contains the main functions and classes for each object
|
86
|
+
|
87
|
+
* `validation` contains type hints for each variable and functions to validate inputs
|
88
|
+
|
89
|
+
* `defaults` contains default settings for each object at different paper sizes
|
90
|
+
|
91
|
+
---
|
92
|
+
|
93
|
+
### North Arrow
|
94
|
+
|
95
|
+
<details>
|
96
|
+
<summary><i>Expand instructions</i></summary>
|
97
|
+
|
98
|
+
#### Quick Start
|
99
|
+
|
100
|
+
Importing the North Arrow functions and classes can be done like so:
|
101
|
+
|
102
|
+
```py
|
103
|
+
from matplotlib_map_utils.core.north_arrow import NorthArrow, north_arrow
|
104
|
+
```
|
105
|
+
|
106
|
+
The quickest way to add a single north arrow to a single plot is to use the `north_arrow` function:
|
107
|
+
|
108
|
+
```python
|
109
|
+
# Setting up a plot
|
110
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
111
|
+
# Adding a north arrow to the upper-right corner of the axis, without any rotation (see Rotation under Formatting Components for details)
|
112
|
+
north_arrow.north_arrow(ax=ax, location="upper right", rotation={"degrees":0})
|
113
|
+
```
|
114
|
+
|
115
|
+
An object-oriented approach is also supported:
|
116
|
+
|
117
|
+
```python
|
118
|
+
# Setting up a plot
|
119
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
120
|
+
# Creating a north arrow for the upper-right corner of the axis, without any rotation (see Rotation under Formatting Components for details)
|
121
|
+
na = north_arrow.NorthArrow(location="upper right", rotation={"degrees":0})
|
122
|
+
# Adding the artist to the plot
|
123
|
+
ax.add_artist(na)
|
124
|
+
```
|
125
|
+
|
126
|
+
Both of these will create an output like the following:
|
127
|
+
|
128
|
+

|
129
|
+
|
130
|
+
#### Customization
|
131
|
+
|
132
|
+
Both the object-oriented and functional approaches can be customized to allow for fine-grained control over formatting:
|
133
|
+
|
134
|
+
```python
|
135
|
+
north_arrow(
|
136
|
+
ax,
|
137
|
+
location = "upper right", # accepts a valid string from the list of locations
|
138
|
+
scale = 0.5, # accepts a valid positive float or integer
|
139
|
+
# each of the follow accepts arguments from a customized style dictionary
|
140
|
+
base = {"facecolor":"green"},
|
141
|
+
fancy = False,
|
142
|
+
label = {"text":"North"},
|
143
|
+
shadow = {"alpha":0.8},
|
144
|
+
pack = {"sep":6},
|
145
|
+
aob = {"pad":2},
|
146
|
+
rotation = {"degrees": 35}
|
147
|
+
)
|
148
|
+
```
|
149
|
+
|
150
|
+
This will create an output like the following:
|
151
|
+
|
152
|
+

|
153
|
+
|
154
|
+
Refer to `docs\howto_north_arrow` for details on how to customize each facet of the north arrow.
|
155
|
+
|
156
|
+
#### Rotation
|
157
|
+
|
158
|
+
The north arrow object is also capable of pointing towards "true north", given a CRS and reference point:
|
159
|
+
|
160
|
+

|
161
|
+
|
162
|
+
Instructions for how to do so can be found in `docs\howto_north_arrow`.
|
163
|
+
</details>
|
164
|
+
|
165
|
+
---
|
166
|
+
|
167
|
+
### Scale Bar
|
168
|
+
|
169
|
+
<details>
|
170
|
+
<summary><i>Expand instructions</i></summary>
|
171
|
+
|
172
|
+
#### Quick Start
|
173
|
+
|
174
|
+
Importing the Scale Bar functions and classes can be done like so:
|
175
|
+
|
176
|
+
```py
|
177
|
+
from matplotlib_map_utils.core.scale_bar import ScaleBar, scale_bar
|
178
|
+
```
|
179
|
+
|
180
|
+
There are two available styles for the scale bars: `boxes` and `ticks`. The quickest way to add one to a single plot is to use the `scale_bar` function:
|
181
|
+
|
182
|
+
```python
|
183
|
+
# Setting up a plot
|
184
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
185
|
+
# Adding a scale bar to the upper-right corner of the axis, in the same projection as whatever geodata you plotted
|
186
|
+
# Here, this scale bar will have the "boxes" style
|
187
|
+
scale_bar(ax=ax, location="upper right", style="boxes", bar={"projection":3857})
|
188
|
+
```
|
189
|
+
|
190
|
+
An object-oriented approach is also supported:
|
191
|
+
|
192
|
+
```python
|
193
|
+
# Setting up a plot
|
194
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
195
|
+
# Adding a scale bar to the upper-right corner of the axis, in the same projection as whatever geodata you plotted
|
196
|
+
# Here, we change the boxes to "ticks"
|
197
|
+
sb = ScaleBar(location="upper right", style="boxes", bar={"projection":3857})
|
198
|
+
# Adding the artist to the plot
|
199
|
+
ax.add_artist(sb)
|
200
|
+
```
|
201
|
+
|
202
|
+
Both of these will create an output like the following (function is left, class is right):
|
203
|
+
|
204
|
+

|
205
|
+
|
206
|
+
#### Customization
|
207
|
+
|
208
|
+
Both the object-oriented and functional approaches can be customized to allow for fine-grained control over formatting:
|
209
|
+
|
210
|
+
```python
|
211
|
+
scale_bar(
|
212
|
+
ax,
|
213
|
+
location = "upper right", # accepts a valid string from the list of locations
|
214
|
+
style = "boxes", # accepts a valid positive float or integer
|
215
|
+
# each of the follow accepts arguments from a customized style dictionary
|
216
|
+
bar = {"unit":"mi", "length":2}, # converting the units to miles, and changing the length of the bar (in inches)
|
217
|
+
labels = {"style":"major", "loc":"below"}, # placing a label on each major division, and moving them below the bar
|
218
|
+
units = {"loc":"text"}, # changing the location of the units text to the major division labels
|
219
|
+
text = {"fontfamily":"monospace"}, # changing the font family of all the text to monospace
|
220
|
+
)
|
221
|
+
```
|
222
|
+
|
223
|
+
This will create an output like the following:
|
224
|
+
|
225
|
+

|
226
|
+
|
227
|
+
Refer to `docs\howto_scale_bar` for details on how to customize each facet of the scale bar.
|
228
|
+
|
229
|
+
</details>
|
230
|
+
|
231
|
+
---
|
232
|
+
|
233
|
+
### Development Notes
|
234
|
+
|
235
|
+
#### Inspiration and Thanks
|
236
|
+
|
237
|
+
This project was heavily inspired by [`matplotlib-scalebar`](https://github.com/ppinard/matplotlib-scalebar/), and much of the code is either directly copied or a derivative of that project, since it uses the same "artist"-based approach.
|
238
|
+
|
239
|
+
Two more projects assisted with the creation of this script:
|
240
|
+
|
241
|
+
* [`EOmaps`](https://github.com/raphaelquast/EOmaps/discussions/231) provided code for calculating the rotation required to point to "true north" for an arbitrary point and CRS for the north arrow.
|
242
|
+
|
243
|
+
* [`Cartopy`](https://github.com/SciTools/cartopy/issues/2361) fixed an issue inherent to calling `.copy()` on `CRS` objects.
|
244
|
+
|
245
|
+
#### Releases
|
246
|
+
|
247
|
+
- `v2.0.1`: Fixed a bug in the `dual_bars()` function that prevented empty dictionaries to be passed. Also added a warning when auto-calculated bar widths appear to be exceeding the dimension of the axis (usually occurs when the axis is <2 kilometeres or miles long, depending on the units selected).
|
248
|
+
|
249
|
+
#### Future Roadmap
|
250
|
+
|
251
|
+
With the release of `v2.x`, and the addition of **Scale Bar** tools, this project has achieved the two main objectives that I set out to.
|
252
|
+
|
253
|
+
If I continue development of this project, I will be looking to add or fix the following features:
|
254
|
+
|
255
|
+
* **North Arrow:**
|
256
|
+
|
257
|
+
* Copy the image-rendering functionality of the Scale Bar to allow for rotation of the entire object, label and arrow together
|
258
|
+
|
259
|
+
* Create more styles for the arrow, potentiallly including a compass rose and a line-only arrow
|
260
|
+
|
261
|
+
* **Scale Bar:**
|
262
|
+
|
263
|
+
* Allow for custom unit definitions (instead of just metres/feet/miles/kilometres/etc.), so that the scale bar can be used on arbitrary plots (such as inches/cm/mm, mathmatical plots, and the like)
|
264
|
+
|
265
|
+
* Fix/improve the `dual_bars()` function, which currently doesn't work great with rotations
|
266
|
+
|
267
|
+
* Clean up the variable naming scheme (consistency on `loc` vs `position`, `style` vs `type`, etc.)
|
268
|
+
|
269
|
+
* Create more styles for the bar, potentiallly including dual boxes and a sawtooth bar
|
270
|
+
|
271
|
+
If that goes well, `v3` can then either create a tool for generating inset maps (which `matplotlib` has *some* support for), or the various functions that I have created in the past that assist with formatting a map "properly", such as centering on a given object and coverting FIPS codes.
|
272
|
+
|
273
|
+
I am also open to ideas for other extensions to create!
|
274
|
+
|
275
|
+
#### Support and Contributions
|
276
|
+
|
277
|
+
If you notice something is not working as intended or if you'd like to add a feature yourself, I welcome PRs - just be sure to be descriptive as to what you are changing and why, including code examples!
|
278
|
+
|
279
|
+
If you are having issues using this script, feel free to leave a post explaining your issue, and I will try and assist, though I have no guaranteed SLAs as this is just a hobby project.
|
280
|
+
|
281
|
+
---
|
282
|
+
|
283
|
+
### License
|
284
|
+
|
285
|
+
I know nothing about licensing, so I went with the GPL license. If that is incompatible with any of the dependencies, please let me know.
|
@@ -0,0 +1,268 @@
|
|
1
|
+
# matplotlib-map-utils
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
**Documentation**: See `docs` folder
|
6
|
+
|
7
|
+
**Source Code**: [Available on GitHub](https://github.com/moss-xyz/matplotlib-map-utils)
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
### Introduction
|
12
|
+
|
13
|
+
`matplotlib_map_utils` is intended to be a package that provides various functions and objects that assist with the the creation of maps using [`matplotlib`](https://matplotlib.org/stable/).
|
14
|
+
|
15
|
+
As of `v2.x` (the current version), this includes two tools:
|
16
|
+
|
17
|
+
* `north_arrow.py`, which generates a high quality, context-aware north arrow for a given plot.
|
18
|
+
|
19
|
+
* `scale_bar.py`, which generates a high quality, context-aware scale bar to a given plot.
|
20
|
+
|
21
|
+
Future releases (if the project is continued) might provide a similar tool inset maps, or other functions that I have created myself that give more control in the formatting of maps.
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
### Installation
|
26
|
+
|
27
|
+
This package is available on PyPi, and can be installed like so:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
pip install matplotlib-map-utils
|
31
|
+
```
|
32
|
+
|
33
|
+
The requirements for this package are:
|
34
|
+
|
35
|
+
* `python >= 3.9.0` (due to the dictionary-concatenation method utilized)
|
36
|
+
|
37
|
+
* `matplotlib >= 3.9.0` (might work with lower versions but not guaranteed)
|
38
|
+
|
39
|
+
* `cartopy >= 0.23.0` (due to earlier bug with calling `copy()` on `CRS` objects)
|
40
|
+
|
41
|
+
---
|
42
|
+
|
43
|
+
### Package Structure
|
44
|
+
|
45
|
+
The package is arrayed in the following way:
|
46
|
+
|
47
|
+
```bash
|
48
|
+
package_name/
|
49
|
+
├── __init__.py
|
50
|
+
│
|
51
|
+
├── core/
|
52
|
+
│ ├── __init__.py
|
53
|
+
│ ├── north_arrow.py
|
54
|
+
│ ├── scale_bar.py
|
55
|
+
├── validation/
|
56
|
+
│ ├── __init__.py
|
57
|
+
│ ├── functions.py
|
58
|
+
│ ├── north_arrow.py
|
59
|
+
│ └── scale_bar.py
|
60
|
+
├── defaults/
|
61
|
+
│ ├── __init__.py
|
62
|
+
│ ├── north_arrow.py
|
63
|
+
│ └── scale_bar.py
|
64
|
+
```
|
65
|
+
|
66
|
+
Where:
|
67
|
+
|
68
|
+
* `core` contains the main functions and classes for each object
|
69
|
+
|
70
|
+
* `validation` contains type hints for each variable and functions to validate inputs
|
71
|
+
|
72
|
+
* `defaults` contains default settings for each object at different paper sizes
|
73
|
+
|
74
|
+
---
|
75
|
+
|
76
|
+
### North Arrow
|
77
|
+
|
78
|
+
<details>
|
79
|
+
<summary><i>Expand instructions</i></summary>
|
80
|
+
|
81
|
+
#### Quick Start
|
82
|
+
|
83
|
+
Importing the North Arrow functions and classes can be done like so:
|
84
|
+
|
85
|
+
```py
|
86
|
+
from matplotlib_map_utils.core.north_arrow import NorthArrow, north_arrow
|
87
|
+
```
|
88
|
+
|
89
|
+
The quickest way to add a single north arrow to a single plot is to use the `north_arrow` function:
|
90
|
+
|
91
|
+
```python
|
92
|
+
# Setting up a plot
|
93
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
94
|
+
# Adding a north arrow to the upper-right corner of the axis, without any rotation (see Rotation under Formatting Components for details)
|
95
|
+
north_arrow.north_arrow(ax=ax, location="upper right", rotation={"degrees":0})
|
96
|
+
```
|
97
|
+
|
98
|
+
An object-oriented approach is also supported:
|
99
|
+
|
100
|
+
```python
|
101
|
+
# Setting up a plot
|
102
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
103
|
+
# Creating a north arrow for the upper-right corner of the axis, without any rotation (see Rotation under Formatting Components for details)
|
104
|
+
na = north_arrow.NorthArrow(location="upper right", rotation={"degrees":0})
|
105
|
+
# Adding the artist to the plot
|
106
|
+
ax.add_artist(na)
|
107
|
+
```
|
108
|
+
|
109
|
+
Both of these will create an output like the following:
|
110
|
+
|
111
|
+

|
112
|
+
|
113
|
+
#### Customization
|
114
|
+
|
115
|
+
Both the object-oriented and functional approaches can be customized to allow for fine-grained control over formatting:
|
116
|
+
|
117
|
+
```python
|
118
|
+
north_arrow(
|
119
|
+
ax,
|
120
|
+
location = "upper right", # accepts a valid string from the list of locations
|
121
|
+
scale = 0.5, # accepts a valid positive float or integer
|
122
|
+
# each of the follow accepts arguments from a customized style dictionary
|
123
|
+
base = {"facecolor":"green"},
|
124
|
+
fancy = False,
|
125
|
+
label = {"text":"North"},
|
126
|
+
shadow = {"alpha":0.8},
|
127
|
+
pack = {"sep":6},
|
128
|
+
aob = {"pad":2},
|
129
|
+
rotation = {"degrees": 35}
|
130
|
+
)
|
131
|
+
```
|
132
|
+
|
133
|
+
This will create an output like the following:
|
134
|
+
|
135
|
+

|
136
|
+
|
137
|
+
Refer to `docs\howto_north_arrow` for details on how to customize each facet of the north arrow.
|
138
|
+
|
139
|
+
#### Rotation
|
140
|
+
|
141
|
+
The north arrow object is also capable of pointing towards "true north", given a CRS and reference point:
|
142
|
+
|
143
|
+

|
144
|
+
|
145
|
+
Instructions for how to do so can be found in `docs\howto_north_arrow`.
|
146
|
+
</details>
|
147
|
+
|
148
|
+
---
|
149
|
+
|
150
|
+
### Scale Bar
|
151
|
+
|
152
|
+
<details>
|
153
|
+
<summary><i>Expand instructions</i></summary>
|
154
|
+
|
155
|
+
#### Quick Start
|
156
|
+
|
157
|
+
Importing the Scale Bar functions and classes can be done like so:
|
158
|
+
|
159
|
+
```py
|
160
|
+
from matplotlib_map_utils.core.scale_bar import ScaleBar, scale_bar
|
161
|
+
```
|
162
|
+
|
163
|
+
There are two available styles for the scale bars: `boxes` and `ticks`. The quickest way to add one to a single plot is to use the `scale_bar` function:
|
164
|
+
|
165
|
+
```python
|
166
|
+
# Setting up a plot
|
167
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
168
|
+
# Adding a scale bar to the upper-right corner of the axis, in the same projection as whatever geodata you plotted
|
169
|
+
# Here, this scale bar will have the "boxes" style
|
170
|
+
scale_bar(ax=ax, location="upper right", style="boxes", bar={"projection":3857})
|
171
|
+
```
|
172
|
+
|
173
|
+
An object-oriented approach is also supported:
|
174
|
+
|
175
|
+
```python
|
176
|
+
# Setting up a plot
|
177
|
+
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
|
178
|
+
# Adding a scale bar to the upper-right corner of the axis, in the same projection as whatever geodata you plotted
|
179
|
+
# Here, we change the boxes to "ticks"
|
180
|
+
sb = ScaleBar(location="upper right", style="boxes", bar={"projection":3857})
|
181
|
+
# Adding the artist to the plot
|
182
|
+
ax.add_artist(sb)
|
183
|
+
```
|
184
|
+
|
185
|
+
Both of these will create an output like the following (function is left, class is right):
|
186
|
+
|
187
|
+

|
188
|
+
|
189
|
+
#### Customization
|
190
|
+
|
191
|
+
Both the object-oriented and functional approaches can be customized to allow for fine-grained control over formatting:
|
192
|
+
|
193
|
+
```python
|
194
|
+
scale_bar(
|
195
|
+
ax,
|
196
|
+
location = "upper right", # accepts a valid string from the list of locations
|
197
|
+
style = "boxes", # accepts a valid positive float or integer
|
198
|
+
# each of the follow accepts arguments from a customized style dictionary
|
199
|
+
bar = {"unit":"mi", "length":2}, # converting the units to miles, and changing the length of the bar (in inches)
|
200
|
+
labels = {"style":"major", "loc":"below"}, # placing a label on each major division, and moving them below the bar
|
201
|
+
units = {"loc":"text"}, # changing the location of the units text to the major division labels
|
202
|
+
text = {"fontfamily":"monospace"}, # changing the font family of all the text to monospace
|
203
|
+
)
|
204
|
+
```
|
205
|
+
|
206
|
+
This will create an output like the following:
|
207
|
+
|
208
|
+

|
209
|
+
|
210
|
+
Refer to `docs\howto_scale_bar` for details on how to customize each facet of the scale bar.
|
211
|
+
|
212
|
+
</details>
|
213
|
+
|
214
|
+
---
|
215
|
+
|
216
|
+
### Development Notes
|
217
|
+
|
218
|
+
#### Inspiration and Thanks
|
219
|
+
|
220
|
+
This project was heavily inspired by [`matplotlib-scalebar`](https://github.com/ppinard/matplotlib-scalebar/), and much of the code is either directly copied or a derivative of that project, since it uses the same "artist"-based approach.
|
221
|
+
|
222
|
+
Two more projects assisted with the creation of this script:
|
223
|
+
|
224
|
+
* [`EOmaps`](https://github.com/raphaelquast/EOmaps/discussions/231) provided code for calculating the rotation required to point to "true north" for an arbitrary point and CRS for the north arrow.
|
225
|
+
|
226
|
+
* [`Cartopy`](https://github.com/SciTools/cartopy/issues/2361) fixed an issue inherent to calling `.copy()` on `CRS` objects.
|
227
|
+
|
228
|
+
#### Releases
|
229
|
+
|
230
|
+
- `v2.0.1`: Fixed a bug in the `dual_bars()` function that prevented empty dictionaries to be passed. Also added a warning when auto-calculated bar widths appear to be exceeding the dimension of the axis (usually occurs when the axis is <2 kilometeres or miles long, depending on the units selected).
|
231
|
+
|
232
|
+
#### Future Roadmap
|
233
|
+
|
234
|
+
With the release of `v2.x`, and the addition of **Scale Bar** tools, this project has achieved the two main objectives that I set out to.
|
235
|
+
|
236
|
+
If I continue development of this project, I will be looking to add or fix the following features:
|
237
|
+
|
238
|
+
* **North Arrow:**
|
239
|
+
|
240
|
+
* Copy the image-rendering functionality of the Scale Bar to allow for rotation of the entire object, label and arrow together
|
241
|
+
|
242
|
+
* Create more styles for the arrow, potentiallly including a compass rose and a line-only arrow
|
243
|
+
|
244
|
+
* **Scale Bar:**
|
245
|
+
|
246
|
+
* Allow for custom unit definitions (instead of just metres/feet/miles/kilometres/etc.), so that the scale bar can be used on arbitrary plots (such as inches/cm/mm, mathmatical plots, and the like)
|
247
|
+
|
248
|
+
* Fix/improve the `dual_bars()` function, which currently doesn't work great with rotations
|
249
|
+
|
250
|
+
* Clean up the variable naming scheme (consistency on `loc` vs `position`, `style` vs `type`, etc.)
|
251
|
+
|
252
|
+
* Create more styles for the bar, potentiallly including dual boxes and a sawtooth bar
|
253
|
+
|
254
|
+
If that goes well, `v3` can then either create a tool for generating inset maps (which `matplotlib` has *some* support for), or the various functions that I have created in the past that assist with formatting a map "properly", such as centering on a given object and coverting FIPS codes.
|
255
|
+
|
256
|
+
I am also open to ideas for other extensions to create!
|
257
|
+
|
258
|
+
#### Support and Contributions
|
259
|
+
|
260
|
+
If you notice something is not working as intended or if you'd like to add a feature yourself, I welcome PRs - just be sure to be descriptive as to what you are changing and why, including code examples!
|
261
|
+
|
262
|
+
If you are having issues using this script, feel free to leave a post explaining your issue, and I will try and assist, though I have no guaranteed SLAs as this is just a hobby project.
|
263
|
+
|
264
|
+
---
|
265
|
+
|
266
|
+
### License
|
267
|
+
|
268
|
+
I know nothing about licensing, so I went with the GPL license. If that is incompatible with any of the dependencies, please let me know.
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# This handles importing of all the functions and classes
|
2
|
+
from .core.north_arrow import NorthArrow, north_arrow
|
3
|
+
from .core.scale_bar import ScaleBar, scale_bar, dual_bars
|
4
|
+
|
5
|
+
# This defines what wildcard imports should import
|
6
|
+
__all__ = ["NorthArrow", "north_arrow", "ScaleBar", "scale_bar", "dual_bars"]
|