Skip to content

Commit 9c18a8b

Browse files
authored
Merge pull request #5358 from ajlien/update-hexbin-map-no-mapbox
create_hexbin_mapbox uses *_map chart types instead of *_mapbox
2 parents 3da153f + 9773b5f commit 9c18a8b

File tree

5 files changed

+56
-37
lines changed

5 files changed

+56
-37
lines changed

doc/apidoc/plotly.figure_factory.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
create_distplot
1919
create_facet_grid
2020
create_gantt
21-
create_hexbin_mapbox
21+
create_hexbin_map
2222
create_ohlc
2323
create_quiver
2424
create_scatterplotmatrix

doc/python/hexbin-mapbox.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,13 @@ jupyter:
3737

3838
This page details the use of a [figure factory](/python/figure-factories/). For more examples with Choropleth maps, see [this page](/python/choropleth-maps/).
3939

40-
In order to use mapbox styles that require a mapbox token, set the token with `plotly.express`. You can also use styles that do not require a mapbox token. See more information on [this page](/python/mapbox-layers/).
41-
4240
```python
4341
import plotly.figure_factory as ff
4442
import plotly.express as px
4543

46-
px.set_mapbox_access_token(open(".mapbox_token").read())
4744
df = px.data.carshare()
4845

49-
fig = ff.create_hexbin_mapbox(
46+
fig = ff.create_hexbin_map(
5047
data_frame=df, lat="centroid_lat", lon="centroid_lon",
5148
nx_hexagon=10, opacity=0.9, labels={"color": "Point Count"},
5249
)
@@ -60,10 +57,9 @@ fig.show()
6057
import plotly.figure_factory as ff
6158
import plotly.express as px
6259

63-
px.set_mapbox_access_token(open(".mapbox_token").read())
6460
df = px.data.carshare()
6561

66-
fig = ff.create_hexbin_mapbox(
62+
fig = ff.create_hexbin_map(
6763
data_frame=df, lat="centroid_lat", lon="centroid_lon",
6864
nx_hexagon=10, opacity=0.5, labels={"color": "Point Count"},
6965
min_count=1,
@@ -77,10 +73,9 @@ fig.show()
7773
import plotly.figure_factory as ff
7874
import plotly.express as px
7975

80-
px.set_mapbox_access_token(open(".mapbox_token").read())
8176
df = px.data.carshare()
8277

83-
fig = ff.create_hexbin_mapbox(
78+
fig = ff.create_hexbin_map(
8479
data_frame=df, lat="centroid_lat", lon="centroid_lon",
8580
nx_hexagon=10, opacity=0.5, labels={"color": "Point Count"},
8681
min_count=1, color_continuous_scale="Viridis",
@@ -97,10 +92,9 @@ import plotly.figure_factory as ff
9792
import plotly.express as px
9893
import numpy as np
9994

100-
px.set_mapbox_access_token(open(".mapbox_token").read())
10195
df = px.data.carshare()
10296

103-
fig = ff.create_hexbin_mapbox(
97+
fig = ff.create_hexbin_map(
10498
data_frame=df, lat="centroid_lat", lon="centroid_lon",
10599
nx_hexagon=10, opacity=0.9, labels={"color": "Average Peak Hour"},
106100
color="peak_hour", agg_func=np.mean, color_continuous_scale="Icefire", range_color=[0,23]
@@ -115,10 +109,9 @@ import plotly.figure_factory as ff
115109
import plotly.express as px
116110
import numpy as np
117111

118-
px.set_mapbox_access_token(open(".mapbox_token").read())
119112
df = px.data.carshare()
120113

121-
fig = ff.create_hexbin_mapbox(
114+
fig = ff.create_hexbin_map(
122115
data_frame=df, lat="centroid_lat", lon="centroid_lon",
123116
nx_hexagon=10, opacity=0.9, labels={"color": "Summed Car.Hours"},
124117
color="car_hours", agg_func=np.sum, color_continuous_scale="Magma"
@@ -133,7 +126,6 @@ import plotly.figure_factory as ff
133126
import plotly.express as px
134127
import numpy as np
135128

136-
px.set_mapbox_access_token(open(".mapbox_token").read())
137129
np.random.seed(0)
138130

139131
N = 500
@@ -150,7 +142,7 @@ frame = np.concatenate([
150142
np.ones(N, int) * i for i in range(n_frames)
151143
])
152144

153-
fig = ff.create_hexbin_mapbox(
145+
fig = ff.create_hexbin_map(
154146
lat=lat, lon=lon, nx_hexagon=15, animation_frame=frame,
155147
color_continuous_scale="Cividis", labels={"color": "Point Count", "frame": "Period"},
156148
opacity=0.5, min_count=1,

plotly/figure_factory/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@
2929

3030
if optional_imports.get_module("pandas") is not None:
3131
from plotly.figure_factory._county_choropleth import create_choropleth
32-
from plotly.figure_factory._hexbin_mapbox import create_hexbin_mapbox
32+
from plotly.figure_factory._hexbin_map import (
33+
create_hexbin_map,
34+
create_hexbin_mapbox,
35+
)
3336
else:
3437

3538
def create_choropleth(*args, **kwargs):
3639
raise ImportError("Please install pandas to use `create_choropleth`")
3740

41+
def create_hexbin_map(*args, **kwargs):
42+
raise ImportError("Please install pandas to use `create_hexbin_map`")
43+
3844
def create_hexbin_mapbox(*args, **kwargs):
3945
raise ImportError("Please install pandas to use `create_hexbin_mapbox`")
4046

@@ -57,6 +63,7 @@ def create_ternary_contour(*args, **kwargs):
5763
"create_distplot",
5864
"create_facet_grid",
5965
"create_gantt",
66+
"create_hexbin_map",
6067
"create_hexbin_mapbox",
6168
"create_ohlc",
6269
"create_quiver",

plotly/figure_factory/_hexbin_mapbox.py renamed to plotly/figure_factory/_hexbin_map.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from plotly.express._core import build_dataframe
22
from plotly.express._doc import make_docstring
3-
from plotly.express._chart_types import choropleth_mapbox, scatter_mapbox
3+
from plotly.express._chart_types import choropleth_map, scatter_map
44
import narwhals.stable.v1 as nw
55
import numpy as np
6+
import warnings
67

78

89
def _project_latlon_to_wgs84(lat, lon):
@@ -322,7 +323,7 @@ def _hexagons_to_geojson(hexagons_lats, hexagons_lons, ids=None):
322323
return dict(type="FeatureCollection", features=features)
323324

324325

325-
def create_hexbin_mapbox(
326+
def create_hexbin_map(
326327
data_frame=None,
327328
lat=None,
328329
lon=None,
@@ -339,7 +340,7 @@ def create_hexbin_mapbox(
339340
opacity=None,
340341
zoom=None,
341342
center=None,
342-
mapbox_style=None,
343+
map_style=None,
343344
title=None,
344345
template=None,
345346
width=None,
@@ -444,9 +445,12 @@ def create_hexbin_mapbox(
444445
)
445446

446447
if range_color is None:
447-
range_color = [agg_data_frame["color"].min(), agg_data_frame["color"].max()]
448+
range_color = [
449+
agg_data_frame["color"].min(),
450+
agg_data_frame["color"].max(),
451+
]
448452

449-
fig = choropleth_mapbox(
453+
fig = choropleth_map(
450454
data_frame=agg_data_frame.to_native(),
451455
geojson=geojson,
452456
locations="locations",
@@ -462,18 +466,20 @@ def create_hexbin_mapbox(
462466
opacity=opacity,
463467
zoom=zoom,
464468
center=center,
465-
mapbox_style=mapbox_style,
469+
map_style=map_style,
466470
title=title,
467471
template=template,
468472
width=width,
469473
height=height,
470474
)
471475

472476
if show_original_data:
473-
original_fig = scatter_mapbox(
477+
original_fig = scatter_map(
474478
data_frame=(
475479
args["data_frame"].sort(
476-
by=args["animation_frame"], descending=False, nulls_last=True
480+
by=args["animation_frame"],
481+
descending=False,
482+
nulls_last=True,
477483
)
478484
if args["animation_frame"] is not None
479485
else args["data_frame"]
@@ -502,8 +508,8 @@ def create_hexbin_mapbox(
502508
return fig
503509

504510

505-
create_hexbin_mapbox.__doc__ = make_docstring(
506-
create_hexbin_mapbox,
511+
create_hexbin_map.__doc__ = make_docstring(
512+
create_hexbin_map,
507513
override_dict=dict(
508514
nx_hexagon=["int", "Number of hexagons (horizontally) to be created"],
509515
agg_func=[
@@ -521,6 +527,20 @@ def create_hexbin_mapbox(
521527
"bool",
522528
"Whether to show the original data on top of the hexbin aggregation.",
523529
],
524-
original_data_marker=["dict", "Scattermapbox marker options."],
530+
original_data_marker=["dict", "Scattermap marker options."],
525531
),
526532
)
533+
534+
535+
def create_hexbin_mapbox(*args, **kwargs):
536+
warnings.warn(
537+
"create_hexbin_mapbox() is deprecated and will be removed in the next major version. "
538+
+ "Please use create_hexbin_map() instead. "
539+
+ "Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
540+
stacklevel=2,
541+
category=DeprecationWarning,
542+
)
543+
if "mapbox_style" in kwargs:
544+
kwargs["map_style"] = kwargs.pop("mapbox_style")
545+
546+
return create_hexbin_map(*args, **kwargs)

tests/test_optional/test_figure_factory/test_figure_factory.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4228,7 +4228,7 @@ def test_aggregation(self):
42284228
lon = [1, 2, 3, 3, 0, 4, 5, 0, 5, 3, 1, 5, 4, 0, 1, 2, 5]
42294229
color = np.ones(len(lat))
42304230

4231-
fig1 = ff.create_hexbin_mapbox(lat=lat, lon=lon, nx_hexagon=1)
4231+
fig1 = ff.create_hexbin_map(lat=lat, lon=lon, nx_hexagon=1)
42324232

42334233
actual_geojson = {
42344234
"type": "FeatureCollection",
@@ -4331,7 +4331,7 @@ def test_aggregation(self):
43314331
self.compare_dict_values(fig1.data[0].geojson, actual_geojson)
43324332
assert np.array_equal(fig1.data[0].z, actual_agg)
43334333

4334-
fig2 = ff.create_hexbin_mapbox(
4334+
fig2 = ff.create_hexbin_map(
43354335
lat=lat,
43364336
lon=lon,
43374337
nx_hexagon=1,
@@ -4341,7 +4341,7 @@ def test_aggregation(self):
43414341

43424342
assert np.array_equal(fig2.data[0].z, np.ones(5))
43434343

4344-
fig3 = ff.create_hexbin_mapbox(
4344+
fig3 = ff.create_hexbin_map(
43454345
lat=np.random.randn(1000),
43464346
lon=np.random.randn(1000),
43474347
nx_hexagon=20,
@@ -4364,8 +4364,8 @@ def test_build_dataframe(self):
43644364
columns=["Latitude", "Longitude", "Metric", "Frame"],
43654365
)
43664366

4367-
fig1 = ff.create_hexbin_mapbox(lat=lat, lon=lon, nx_hexagon=nx_hexagon)
4368-
fig2 = ff.create_hexbin_mapbox(
4367+
fig1 = ff.create_hexbin_map(lat=lat, lon=lon, nx_hexagon=nx_hexagon)
4368+
fig2 = ff.create_hexbin_map(
43694369
data_frame=df, lat="Latitude", lon="Longitude", nx_hexagon=nx_hexagon
43704370
)
43714371

@@ -4375,22 +4375,22 @@ def test_build_dataframe(self):
43754375
fig1.to_plotly_json()["data"][0], fig2.to_plotly_json()["data"][0]
43764376
)
43774377

4378-
fig3 = ff.create_hexbin_mapbox(
4378+
fig3 = ff.create_hexbin_map(
43794379
lat=lat,
43804380
lon=lon,
43814381
nx_hexagon=nx_hexagon,
43824382
color=color,
43834383
agg_func=np.sum,
43844384
min_count=0,
43854385
)
4386-
fig4 = ff.create_hexbin_mapbox(
4386+
fig4 = ff.create_hexbin_map(
43874387
lat=lat,
43884388
lon=lon,
43894389
nx_hexagon=nx_hexagon,
43904390
color=color,
43914391
agg_func=np.sum,
43924392
)
4393-
fig5 = ff.create_hexbin_mapbox(
4393+
fig5 = ff.create_hexbin_map(
43944394
data_frame=df,
43954395
lat="Latitude",
43964396
lon="Longitude",
@@ -4406,7 +4406,7 @@ def test_build_dataframe(self):
44064406
fig4.to_plotly_json()["data"][0], fig5.to_plotly_json()["data"][0]
44074407
)
44084408

4409-
fig6 = ff.create_hexbin_mapbox(
4409+
fig6 = ff.create_hexbin_map(
44104410
data_frame=df,
44114411
lat="Latitude",
44124412
lon="Longitude",
@@ -4416,7 +4416,7 @@ def test_build_dataframe(self):
44164416
animation_frame="Frame",
44174417
)
44184418

4419-
fig7 = ff.create_hexbin_mapbox(
4419+
fig7 = ff.create_hexbin_map(
44204420
lat=lat,
44214421
lon=lon,
44224422
nx_hexagon=nx_hexagon,

0 commit comments

Comments
 (0)