Skip to content

Commit

Permalink
Merge pull request #586 from GAA-UAM/feature/basemap_to_cartopy
Browse files Browse the repository at this point in the history
Change example from Basemap to Cartopy.
  • Loading branch information
vnmabus committed Oct 15, 2023
2 parents 9f74e24 + 3e74952 commit 9fd6c39
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
81 changes: 33 additions & 48 deletions examples/full_examples/plot_aemet_unsupervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

from typing import Any, Mapping, Tuple

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import sklearn.cluster
from cartopy.io.img_tiles import GoogleTiles
from matplotlib.axes import Axes
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure

from skfda.datasets import fetch_aemet
from skfda.exploratory.depth import ModifiedBandDepth
Expand Down Expand Up @@ -68,8 +70,8 @@
##############################################################################
# We want to plot the cluster of each station in the map of Spain. We need to
# define first auxiliary variables and functions for plotting.
coords_spain = (-10, 34.98, 5, 44.8)
coords_canary = (-18.5, 27.5, -13, 29.5)
coords_spain = (-10, 5, 34.98, 44.8)
coords_canary = (-18.5, -13, 27.5, 29.5)

# It is easier to obtain the longitudes and latitudes from the data in
# a Pandas dataframe.
Expand All @@ -81,44 +83,39 @@

def create_map(
coords: Tuple[float, float, float, float],
ax: Axes,
) -> Basemap:
figsize: Tuple[float, float],
) -> Figure:
"""Create a map for a region of the world."""
basemap = Basemap(
*coords,
projection='merc',
resolution="h",
epsg=4326,
ax=ax,
fix_aspect=False,
)
basemap.arcgisimage(
service='World_Imagery',
xpixels=1000,
dpi=100,
)
tiler = GoogleTiles(style="satellite")
mercator = tiler.crs

return basemap
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1], projection=mercator)
ax.set_extent(coords, crs=ccrs.PlateCarree())

ax.add_image(tiler, 8)
ax.set_adjustable('datalim')

return fig


def plot_cluster_points(
longitudes: np.typing.NDArray[np.floating[Any]],
latitudes: np.typing.NDArray[np.floating[Any]],
clusters: np.typing.NDArray[np.integer[Any]],
color_map: Mapping[int, str],
basemap: Basemap,
ax: Axes,
) -> None:
"""Plot the stations in a map with their cluster color."""
x, y = basemap(longitudes, latitudes)
for cluster in range(n_clusters):
selection = (clusters == cluster)
ax.scatter(
x[selection],
y[selection],
longitudes[selection],
latitudes[selection],
s=64,
color=color_map[cluster],
edgecolors='white',
transform=ccrs.Geodetic(),
)


Expand All @@ -144,29 +141,23 @@ def plot_cluster_points(
# We now plot the obtained clustering in the maps.

# Mainland
fig_spain = plt.figure(figsize=(8, 6))
ax_spain = fig_spain.add_axes([0, 0, 1, 1])
map_spain = create_map(coords_spain, ax=ax_spain)
fig_spain = create_map(coords_spain, figsize=(8, 6))
plot_cluster_points(
longitudes=station_longitudes,
latitudes=station_latitudes,
clusters=fda_clusters,
color_map=fda_color_map,
basemap=map_spain,
ax=ax_spain,
ax=fig_spain.axes[0],
)

# Canary Islands
fig_canary = plt.figure(figsize=(8, 3))
ax_canary = fig_canary.add_axes([0, 0, 1, 1])
map_canary = create_map(coords_canary, ax=ax_canary)
fig_canary = create_map(coords_canary, figsize=(8, 3))
plot_cluster_points(
longitudes=station_longitudes,
latitudes=station_latitudes,
clusters=fda_clusters,
color_map=fda_color_map,
basemap=map_canary,
ax=ax_canary,
ax=fig_canary.axes[0],
)
plt.show()

Expand Down Expand Up @@ -228,36 +219,30 @@ def plot_cluster_points(
# color map to match cluster colors with the previously obtained ones.

mv_color_map = {
0: "red",
1: "purple",
2: "yellow",
3: "green",
4: "orange",
0: "yellow",
1: "orange",
2: "red",
3: "purple",
4: "green",
}

# Mainland
fig_spain = plt.figure(figsize=(8, 6))
ax_spain = fig_spain.add_axes([0, 0, 1, 1])
map_spain = create_map(coords_spain, ax=ax_spain)
fig_spain = create_map(coords_spain, figsize=(8, 6))
plot_cluster_points(
longitudes=station_longitudes,
latitudes=station_latitudes,
clusters=mv_clusters,
color_map=mv_color_map,
basemap=map_spain,
ax=ax_spain,
ax=fig_spain.axes[0],
)

# Canary Islands
fig_canary = plt.figure(figsize=(8, 3))
ax_canary = fig_canary.add_axes([0, 0, 1, 1])
map_canary = create_map(coords_canary, ax=ax_canary)
fig_canary = create_map(coords_canary, figsize=(8, 3))
plot_cluster_points(
longitudes=station_longitudes,
latitudes=station_latitudes,
clusters=mv_clusters,
color_map=mv_color_map,
basemap=map_canary,
ax=ax_canary,
ax=fig_canary.axes[0],
)
plt.show()
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ dependencies = [

[project.optional-dependencies]
docs = [
"basemap",
"basemap-data",
"basemap-data-hires",
"cartopy",
"ipykernel",
"jupyter-sphinx",
"myst-parser",
Expand Down

0 comments on commit 9fd6c39

Please sign in to comment.