Geographic Paths and Labels#
A geographic plot connecting two locations using paths()
and labels()
. This example demonstrates how to compose map-based annotations.
Notice how the line in blue between New York and Delhi is not straight on a flat PlateCarree
map, this is because the Geodetic
coordinate system is a truly spherical coordinate system, where a line between two points is defined as the shortest path between those points on the globe rather than 2d Cartesian space.
import pandas as pd
import hvplot.pandas # noqa
import cartopy.crs as ccrs
df = pd.DataFrame({
'city': ['New York', 'Delhi'],
'lon': [-74.006, 77.1025],
'lat': [40.7128, 28.7041]
})
# Shared plot args
common_kwargs = dict(
x='lon', y='lat', geo=True,
projection=ccrs.PlateCarree(), global_extent=True,
)
# Create paths, points, and labels
shortest_path = df.hvplot.paths(
color='blue', crs=ccrs.Geodetic(),
features=['coastline'], **common_kwargs
)
straight_path = df.hvplot.paths(
color='grey', crs=ccrs.PlateCarree(), line_dash='dashed',
line_width=2, **common_kwargs
)
points = df.hvplot.points(color='red', size=10, **common_kwargs)
labels = df.hvplot.labels(
text='city', text_baseline='bottom',
text_color='black', text_align='left', **common_kwargs
)
# Compose the final plot
shortest_path * straight_path * points * labels
import pandas as pd
import hvplot.pandas # noqa
import cartopy.crs as ccrs
hvplot.extension('matplotlib')
df = pd.DataFrame({
'city': ['New York', 'Delhi'],
'lon': [-74.006, 77.1025],
'lat': [40.7128, 28.7041]
})
# Shared plot args
common_kwargs = dict(
x='lon', y='lat', geo=True, width=700,
projection=ccrs.PlateCarree(), global_extent=True,
)
# Create paths, points, and labels
shortest_path = df.hvplot.paths(
color='blue', crs=ccrs.Geodetic(),
features=['coastline'], **common_kwargs
)
straight_path = df.hvplot.paths(
color='grey', crs=ccrs.PlateCarree(),
linestyle='dashed', linewidth=2, **common_kwargs
)
points = df.hvplot.points(color='red', size=1000, **common_kwargs)
labels = df.hvplot.labels(
text='city', verticalalignment='bottom', horizontalalignment='left',
color='black', size=300, **common_kwargs
)
# Compose the final plot
shortest_path * straight_path * points * labels
Example adapted from https://scitools.org.uk/cartopy/docs/latest/matplotlib/intro.html.
This web page was generated from a Jupyter notebook and not all
interactivity will work on this website.