plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
15.77k stars 2.52k forks source link

Bug: Error in ff.create_hexbin_mapbox #3834

Open ragul-n opened 1 year ago

ragul-n commented 1 year ago

Getting the following error while creating Hexbins in Mapbox using Plotly figure factory.


ValueError                                
Traceback (most recent call last)
<ipython-input-8-a36bc32c0a84> in <module>
      5 #df = hab[hab['STATE_ID'] ==2].dropna()
      6 
----> 7 fig = ff.create_hexbin_mapbox(
      8     data_frame=df, lat=df["longitude"].astype("double"), lon=df["latitude"].astype("double"), color=df['TOT_POPULA'].astype("double"),agg_func=np.sum,nx_hexagon=10,
      9     opacity=0.7, labels={"color": "Population"},

c:\ProgramData\Anaconda3\lib\site-packages\plotly\figure_factory\_hexbin_mapbox.py in create_hexbin_mapbox(data_frame, lat, lon, color, nx_hexagon, agg_func, animation_frame, color_discrete_sequence, color_discrete_map, labels, color_continuous_scale, range_color, color_continuous_midpoint, opacity, zoom, center, mapbox_style, title, template, width, height, min_count, show_original_data, original_data_marker)
    352     lon_range = args["data_frame"][args["lon"]].agg(["min", "max"]).values
    353 
--> 354     hexagons_lats, hexagons_lons, hexagons_ids, count = _compute_wgs84_hexbin(
    355         lat=args["data_frame"][args["lat"]].values,
    356         lon=args["data_frame"][args["lon"]].values,
c:\ProgramData\Anaconda3\lib\site-packages\plotly\figure_factory\_hexbin_mapbox.py in _compute_wgs84_hexbin(lat, lon, lat_range, lon_range, color, nx, agg_func, min_count)
    280     x_range, y_range = _project_latlon_to_wgs84(lat_range, lon_range)
    281 
--> 282     hxs, hys, centers, agreggated_value = _compute_hexbin(
    283         x, y, x_range, y_range, color, nx, agg_func, min_count
    284     )

c:\ProgramData\Anaconda3\lib\site-packages\plotly\figure_factory\_hexbin_mapbox.py in _compute_hexbin(x, y, x_range, y_range, color, nx, agg_func, min_count)
      136 
 ...
--> 138         lattice1 = np.zeros((nx1, ny1))
      139         lattice2 = np.zeros((nx2, ny2))
      140         c1 = (0 <= ix1) & (ix1 < nx1) & (0 <= iy1) & (iy1 < ny1) & bdist
ValueError: negative dimensions are not allowed  

Code:

import pandas as pd
import plotly.figure_factory as ff
import plotly.express as px 

df= pd.read_csv("sample_data.csv")

px.set_mapbox_access_token(open("mapbox_token").read())

fig = ff.create_hexbin_mapbox(
    data_frame=df, lat="latitude", lon="longitude", color='TOT_POPULA',
    agg_func=np.sum,  nx_hexagon=30,
    opacity=0.7, labels={"color": "Population"},
    show_original_data=False,
    original_data_marker=dict(size=1, opacity=0.6, color="deeppink"),
    min_count=1
)
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()

Here is the dataset used above: sample_data.csv

mayukhnair commented 1 year ago

@RenaudLN @nicolaskruchten Hi, since I see your contributions in PRs #2559 #2638, would it be possible to have a dekko here? We have been unable to figure out the root cause and haven't received any response on the Plotly community forum/Stack Overflow either.

RenaudLN commented 1 year ago

@ragul-n you've got incorrect latitude and longitude values

image

This works

import pandas as pd
import plotly.figure_factory as ff
import plotly.express as px 

df = pd.read_csv("sample_data.csv")

fig = ff.create_hexbin_mapbox(
    data_frame=df.query("-90 <= latitude <= 90 & -180 <= longitude <= 180"), lat="latitude", lon="longitude", color='TOT_POPULA',
    agg_func=np.sum,  nx_hexagon=30,
    opacity=0.7, labels={"color": "Population"},
    show_original_data=False,
    original_data_marker=dict(size=1, opacity=0.6, color="deeppink"),
    min_count=1,
    mapbox_style="carto-darkmatter",
)
fig.update_layout(margin=dict(b=0, t=0, l=0, r=0))
fig.show()

image