在 pycharm 中使用 folium 绘制等高线图

Contour plot using folium in pycharm

我有一个包含纬度、经度和温度的数据。 我需要根据 folium 中的纬度和经度绘制 temp 的等高线图。我试过这个 .. 但根本无法生成地图。 请任何人帮助我找到解决方案。

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)

提前致谢 我还添加了我尝试过的下一个代码 列表=sample_data["温度"].tolist() m=folium.Map(位置=[plot.lat.mean(),plot.lon.mean()],zoom_start=6) 热图(列表).add_to(m) m.save("out.html")

但没有任何效果..它还给出了一个错误 float not subscriptable'

  • 使用了地震数据,因为它有纬度、经度和震级
  • 原来理解是要了heatmap,所以收录了
  • 重复使用了之前的答案:
  • 已意识到它存在问题。 meshgrid()需要用linear插值点https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata
  • 还包括实际积分
  • 现在轮廓线有意义了。我希望温度数据同样有效。因此,我在数据框中使用常量作为列的原因
  • 进一步证明它适用于温度数据。数据来自英国气象站,等高线几何在英国范围内
import 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

输出(剪裁)

  • UIK温度数据