Skip to content
Advertisement

Map with bubbles with radius on basis of counting

I have this easy script for creating bubbles on map, but I would like bubbles with radius on basis of intensity. I put an image with lat, lon and count. In finaly I put the map result, but there aren’t bubbles. Where I am wrong?

import pandas as pd
# Load the dataset into a pandas dataframe.
df = pd.read_csv("autostrada_a_6.csv", delimiter=';', error_bad_lines=False)
# Report the number of sentences.
print('Number of sentences: {:,}n'.format(df.shape[0]))
df.head()
locations = df.groupby(by=['latitudine','longitudine'])
       .count()['created_at']
       .sort_values(ascending=False)
locations

enter image description here

    # Make an empty map
    m = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=2)
    def get_radius(freq):
        if freq < 5:
          return 0.1
        elif freq < 15:
          return 0.5
        elif freq < 257:
          return 1.0
  for i,x in locations.iteritems():
   folium.CircleMarker(
      location=[i[0], i[1]],
      radius=get_radius(x),
      color='crimson',
      fill=True,
      fill_color='crimson'
   ).add_to(m)

enter image description here dataset

Advertisement

Answer

The reason for the display seems to be that the default value for the bubble radius is 10, so the size of the bubble is indistinguishable because it is specified below that value. So, change the return value to determine the radius from a certain value. And to give markers at individual positions, we need to get the data in rows, so we are changing to locations.iterrows().

import folium

m = folium.Map(location=[df['latitudine'].mean(), df['longitudine'].mean()], tiles="OpenStreetMap", zoom_start=8)

def get_radius(freq):
    if freq < 5:
        return 5
    elif freq < 15:
        return 15
    elif freq < 257:
          return 45
        
for i,row in locations.iterrows():
    #print(i,row)
    folium.CircleMarker(
      location=[row[0], row[1]],
      radius=get_radius(row[2]),
      color='crimson',
      fill=True,
      fill_color='crimson'
    ).add_to(m)

m

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement