mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-18 12:30:35 +08:00
* Adding an example of matplotlib-scalebar * Updating version number of matplotlib-scalebar library * DOC: Resolving comments for matplotlib-scalebar example * formatting, icon Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
6.7 KiB
6.7 KiB
In [ ]:
import geopandas as gpd
from matplotlib_scalebar.scalebar import ScaleBarIn [ ]:
nybb = gpd.read_file(gpd.datasets.get_path('nybb'))
nybb = nybb.to_crs(32619) # Convert the dataset to a coordinate
# system which uses meters
ax = nybb.plot()
ax.add_artist(ScaleBar(1))In [ ]:
from shapely.geometry.point import Point
points = gpd.GeoSeries([Point(-73.5, 40.5), Point(-74.5, 40.5)], crs=4326) # Geographic WGS 84 - degrees
points = points.to_crs(32619) # Projected WGS 84 - metersIn [ ]:
distance_meters = points[0].distance(points[1])In [ ]:
nybb = gpd.read_file(gpd.datasets.get_path('nybb'))
nybb = nybb.to_crs(4326) # Using geographic WGS 84
ax = nybb.plot()
ax.add_artist(ScaleBar(distance_meters))In [ ]:
nybb = gpd.read_file(gpd.datasets.get_path('nybb'))
ax = nybb.plot()
ax.add_artist(ScaleBar(1, dimension="imperial-length", units="ft"))In [ ]:
nybb = gpd.read_file(gpd.datasets.get_path('nybb')).to_crs(32619)
ax = nybb.plot()
# Position and layout
scale1 = ScaleBar(
dx=1, label='Scale 1',
location='upper left', # in relation to the whole plot
label_loc='left', scale_loc='bottom' # in relation to the line
)
# Color
scale2 = ScaleBar(
dx=1, label='Scale 2', location='center',
color='#b32400', box_color='yellow',
box_alpha=0.8 # Slightly transparent box
)
# Font and text formatting
scale3 = ScaleBar(
dx=1, label='Scale 3',
font_properties={'family':'serif', 'size': 'large'}, # For more information, see the cell below
scale_formatter=lambda value, unit: f'> {value} {unit} <'
)
ax.add_artist(scale1)
ax.add_artist(scale2)
ax.add_artist(scale3)