I have a data with latitude, longitude and temp. I need to draw a contour plot for temp based on the latitude and longitude in folium. I tried this one ..but nothing works not producing a map at all. Please anybody help me to get a solution.
sample_data=pd.read_csv('cont.csv',delim_whitespace=True)
plot=sample_data[["lat","lon","Temp"]]
map=folium.Map(location=[plot.lat.mean(),plot.lon.mean()],
zoom_start=14,control_scale=True)
Thanks in advance I'm also adding the next code I tried List=sample_data["Temp"].tolist() m=folium.Map(Location=[plot.lat.mean(),plot.lon.mean()],zoom_start=6) HeatMap(list).add_to(m) m.save("out.html")
But nothing works .. It also giving an error float not subscriptable'
meshgrid()
needs to use linear
to interpolate points https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddataimport pandas as pd
import numpy as np
import folium
from folium.plugins import HeatMap, HeatMapWithTime
from scipy.interpolate import griddata
from matplotlib import pyplot as plt
import geojsoncontour
import geopandas as gpd
import json, requests, io
earthquakes = False
if earthquakes:
df = pd.read_csv(
"https://earthquake.usgs.gov/fdsnws/event/1/query.csv?starttime=2021-09-18%2000:00:00&endtime=2022-01-17%2023:59:59&minmagnitude=2.5&orderby=time"
)
df["time"] = pd.to_datetime(df["time"])
# df = df.loc[df["time"].dt.date.eq(df.groupby(df["time"].dt.to_period("D")).size().idxmax().to_timestamp())]
MEASURE="mag"
LON="longitude"
LAT="latitude"
else:
df = pd.read_csv(io.StringIO("""lat,lon,T
60.749,-0.854,7.4
60.139,-1.183,6.8
59.527,-1.628,7.6
58.288,-4.442,2.2
56.867,-4.708,3.7
54.721,-6.814,1.5
52.242,-2.885,-0.8
52.48,-1.689,2.7
52.225,-0.464,3.1
51.86,-1.692,1.3
54.803,-4.008,5.5
55.627,-3.735,4.0
54.572,-2.413,0.5
55.285,-2.279,0.9
55.02,-1.88,3.9
55.1604,-6.9492,3.2
51.201,-1.805,2.3
53.811,-1.865,1.7
53.031,-0.502,4.3
53.088,0.274,0.9
52.148,-2.04,-0.3
54.614,-3.157,1.2
53.497,-3.056,3.1
52.757,-3.464,3.1
52.949,1.127,3.8
57.077,-2.836,-1.8
56.852,-2.264,3.7
57.725,-4.896,-0.4
50.89,0.319,4.2
54.707,-7.577,1.9
53.12755,-1.97993,2.6
50.354,-4.121,4.0
54.237,-6.502,2.1
52.358,-1.33,0.8
53.093,-3.941,0.6
58.954,-2.9,7.3
56.326,-3.729,5.8
50.7366,-3.40458,0.8"""))
MEASURE="T"
LON="lon"
LAT="lat"
# basic map..
m = folium.Map(location=[df[LAT].mean(), df[LON].mean()], zoom_start=2)
# create a heatmap of where measures are
HeatMap(
df.loc[:, [LAT, LON]].values, name="heatmap"
).add_to(m)
temp = df[MEASURE]
lon = df[LON]
lat = df[LAT]
x = np.linspace(min(lon), max(lon), 100)
y = np.linspace(min(lat), max(lat), 100)
X, Y = np.meshgrid(x, y)
Z = griddata((lon, lat), temp, (X, Y), method="linear")
contour = plt.contour(X, Y, Z)
# plot contour lines on folium
gdf = gpd.GeoDataFrame.from_features(
json.loads(
geojsoncontour.contour_to_geojson(
contour=contour, min_angle_deg=3.0, ndigits=5, stroke_width=1
)
)
).set_crs("EPSG:4326")
m = gdf.explore(m=m, color=gdf["stroke"], name="contour")
# add to folium where measures are...
m = gpd.GeoDataFrame(
geometry=gpd.points_from_xy(df[LON], df[LAT]),
data=df[MEASURE],
crs="epsg:4326",
).explore(m=m, column=MEASURE, cmap="hot", name="locations")
folium.LayerControl().add_to(m)
m