Search code examples
pythonplotlymapboxopenstreetmapplotly-python

How to get the radius of the biggest bubble python plotly open street map?


To do my furthur analysis i want get the radius of the biggest bubble?it's better if I can get another column with a bubble radius.

Current code:

fig = px.scatter_mapbox(df, lat="GPSLat", lon="GPSLng", zoom=15, height=500,width=1000,
                        size="Count",color="Device",title=' All device :2021/01/08')
fig.update_layout(mapbox_style="satellite") 
fig.show()

df:

Device  GPSLat  GPSLng  Count
1001    6.8050  80.0154 9.0
1001    6.6050  80.2154 12.0
1001    6.7050  80.4154 114.0
1002    6.8050  80.0154 2.0
1001    6.5050  80.0154 2111.0

Solution

  • As commented, the diameter or radius will be automatically calculated and drawn. If you get the drawing data, you will see that the bubble size is based on your 'Count'.

    import plotly.express as px
    import pandas as pd
    import numpy as np
    import io
    
    data = '''
    Device  GPSLat  GPSLng  Count
    1001    6.8050  80.0154 509.0
    1001    6.6050  80.2154 1200.0
    1001    6.7050  80.4154 840.0
    1002    6.0050  80.0154 1602.0
    1001    6.5050  80.0154 2111.0
    '''
    df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    
    import plotly.express as px
    
    px.set_mapbox_access_token(open("mapbox_api_key.txt").read())
    fig = px.scatter_mapbox(df,
                            lat="GPSLat",
                            lon="GPSLng",
                            hover_name='Device',
                            height=500, width=1000,
                            zoom=8,
                            size="Count",
                            color="Count",
                            title=" All device :2021/01/08",
                            mapbox_style='satellite')
    
    fig.show()
    

    enter image description here

    fig.data
    (Scattermapbox({
         'hovertemplate': ('<b>%{hovertext}</b><br><br>Cou' ... 'r>GPSLng=%{lon}<extra></extra>'),
         'hovertext': array([1001., 1001., 1001., 1002., 1001.]),
         'lat': array([6.805, 6.605, 6.705, 6.005, 6.505]),
         'legendgroup': '',
         'lon': array([80.0154, 80.2154, 80.4154, 80.0154, 80.0154]),
         'marker': {'color': array([ 509., 1200.,  840., 1602., 2111.]),
                    'coloraxis': 'coloraxis',
                    'size': array([ 509., 1200.,  840., 1602., 2111.]),
                    'sizemode': 'area',
                    'sizeref': 5.2775},
         'mode': 'markers',
         'name': '',
         'showlegend': False,
         'subplot': 'mapbox'
     }),)
    
    # marker size
    fig.data[0]['marker']['size']
    array([ 509., 1200.,  840., 1602., 2111.])