-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday25_interactive.py
93 lines (81 loc) · 2.64 KB
/
day25_interactive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:hydrogen
# text_representation:
# extension: .py
# format_name: hydrogen
# format_version: '1.3'
# jupytext_version: 1.9.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# # Day 25 : Interactive map
#
# What if the user wants to click, pan and explore your map?
# %%
import panel as pn
import param
import pygmt
import xarray as xr
pn.extension()
# %% [markdown]
# ## Make panel widgets
#
# [Panel](https://panel.holoviz.org/index.html) is a Python library
# for connecting interactive widgets with plots! We'll use Panel with
# [PyGMT](https://www.pygmt.org) to explore different colour maps and
# projections for the Earth
#
# - Scientific Colour Maps in GMT: https://docs.generic-mapping-tools.org/6.2/cookbook/cpts.html#id2
# - Projections: https://www.pygmt.org/v0.5.0/projections
# %%
cmap = pn.widgets.RadioButtonGroup(
name="Scientific Colour Maps",
options=["batlow", "bukavu", "fes", "oleron"],
button_type="success",
sizing_mode="scale_width",
)
projection = pn.widgets.RadioButtonGroup(
name="Projection Type",
options={
"Hammer": "H",
"Sinusoidal": "I",
"Miller Cylindrical": "J",
"Robinson": "N",
"Winkel Tripel": "R",
"Mollweide": "W",
},
)
# %% [markdown]
# ## Load earth_relief data and create plot function
#
# Getting SRTM15+V2.1 [earth_relief](https://docs.generic-mapping-tools.org/6.2/datasets/remote-data.html#global-earth-relief-grids)
# via [`pygmt.datasets.load_earth_relief`](https://www.pygmt.org/v0.5.0/api/generated/pygmt.datasets.load_earth_relief.html),
# and making a simple PyGMT `grdimage` function that takes a cmap
# and projection string as input.
# %%
grid: xr.DataArray = pygmt.datasets.load_earth_relief(resolution="01d")
# %%
@pn.depends(cmap.param.value, projection)
def view(cmap: str, projection: str):
fig = pygmt.Figure()
fig.grdimage(grid=grid, cmap=cmap, projection=f"{projection}180/20c")
return fig
# %% [markdown]
# ## Make the interactive dashboard!
#
# Now to put everything together! The 'dashboard' will be very simple.
# The 'cmap' and 'projection' radio button chooser buttons is placed
# one after another vertically using `panel.Column`, and the map is
# then draw below. Toggling different buttons will update the map
# to use a new colormap or projection! For more info, go check out
# https://panel.holoviz.org/getting_started/index.html#using-panel
#
# Note: This is meant to run in a Jupyter lab/notebook environment.
# %%
pn.Column(cmap, projection, view)