1

I'm not able to see my data on the map when I run the following script. I can see the map, the temporal slider is present at the bottom and scrolls through the dates I provided, however, I do not see a heat signature at any of the locations. Is there something I'm leaving off of this?

This is the table I'm working with: enter image description here

# HEATMAP OVER TIME WITH MY DATA
import folium
from folium import plugins
import pandas as pd

ASOS_DATA = r"C:\Users\ASOS_Cali_Weather_Stations.csv"
df = pd.read_csv(ASOS_DATA)
latlon = (df[["lon", "lat"]]).values.tolist()
date = (df["test_date"]).values.tolist()

# MAP
map_heatmap_time = folium.Map([37, -122], tiles='CartoDB Dark_Matter', zoom_start = 6)

# HEATMAP PLUGIN
heatmap_time_plugin = plugins.HeatMapWithTime(latlon, index= date)

# ADD HEATMAP PLUGIN TO MAP
heatmap_time_plugin.add_to(map_heatmap_time)

# DISPLAY THE MAP
map_heatmap_time
Geno
  • 29
  • 7
  • You updated the sample data after my response, and so my response is unacceptable because I don't have the data you presented. That is not reasonable. – r-beginners Nov 25 '22 at 12:22
  • r-beginners, greatly appreciate your response and assistance! I had needed a solution that would use the columns I had in place. Your answer was extremely helpful, though, I ended up not being able to use it because it made a list of randomized data as one of the inputs. It would work to create a heatmap, but for my purposes, I needed a script that would use my data for the heatmap. Thank you again for your help! – Geno Dec 07 '22 at 23:30

2 Answers2

1

Since there is no data presented, I created a graph using sample data. The time period is 30 days, and there are 30 latitude and longitude locations in date units. That is the data for the heatmap, and it is a multiple list. I now have 30 latitude/longitude and heatmap values ready for one day in the date slider. Set the created data and the date list and you are done.

import folium
import folium.plugins as plugins
import pandas as pd
import numpy as np
import random

# sample data
df = pd.DataFrame({'test_date': np.repeat(pd.date_range('2022-09-01', periods=30), 30),
                   'lon': [random.uniform(36.5, 37.5) for _ in range(900)],
                   'lat':[random.uniform(-121.5, -122.5) for _ in range(900)],
                   'value': np.random.rand(900)})
df['test_date'] = pd.to_datetime(df['test_date'])

# heatmap data by date
latlon = []
for d in df['test_date'].unique():
    dff = df.query('test_date == @d')
    latlon.append(dff[['lon','lat','value']].values.tolist())

# convert date to str(date)   
date = [k.strftime("%Y-%m-%d") for k in pd.date_range('2022-09-01', periods=30)]

map_heatmap_time = folium.Map([37, -122], tiles='OpenStreetMap', zoom_start=8)

heatmap_time_plugin = plugins.HeatMapWithTime(latlon, index=date)

heatmap_time_plugin.add_to(map_heatmap_time)

map_heatmap_time

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Thanks r-beginners! I had seen this on a similar post but was going to try to use this script for some data I had. Sorry, I hadn't included that on my first post. Just made an edit to the original question so you can see the format I have the data in. Appreciate any feedback you can offer! – Geno Nov 11 '22 at 00:07
  • Try it with the data you have. folium's heatmap is a density heatmap, so it heatmaps density and value, including adjacent data. Since we don't know the full extent of your data, it is unclear if we can produce the intended heatmap. – r-beginners Nov 11 '22 at 01:06
  • Thanks r-beginners, appreciate you working this up! I found a similar script online that I can run and get a heatmap with, but would like to be able to replicate this exercise with some data I have. You're code will work on my end but was hoping to work up a script so I can reference some cells in the csv I have on my end. – Geno Nov 15 '22 at 04:31
0

This seems to do the trick. Combined some code snippets from the following 2 links (https://www.youtube.com/watch?v=t9Ed5QyO7qY and https://blog.jovian.ai/interesting-heatmaps-using-python-folium-ee41b118a996#154e)

import folium
from folium import plugins
import pandas as pd

ASOS_DATA = r"C:\Users\ASOS_Cali_Weather_Stations.xlsx"
df = pd.read_excel(ASOS_DATA)

time_index= list(df["test_date"].sort_values().astype('str').unique())
df["test_date"] = df["test_date"].sort_values(ascending=True)

data = []
for _, d in df.groupby("test_date"):
    data.append([[row['lat'], row["lon"], row["norm_temp"]] for _,row in d.iterrows()])

map_heatmap_time = folium.Map([37, -122], tiles='CartoDB Dark_Matter', zoom_start = 7)
   
heatmap_time_plugin = plugins.HeatMapWithTime(data, index= time_index)

heatmap_time_plugin.add_to(map_heatmap_time)

map_heatmap_time
Geno
  • 29
  • 7