我想在plotly
density_mapbox
Map上添加天气等值线,但不确定必要的步骤。
首先,我创建了一个matplotlib
等值线图来可视化数据。
然后,我使用geojsoncontour
从上述轮廓的matplotlib
轮廓图创建geojson
文件。
我现在要做的是在与density_mapbox
相同的Map中绘制等高线。geojson
和包含数据的.csv文件可以在here中找到。
关于.csv文件,“兰德_Data”是进入density_mapbox
图的数据,“Rain_in”是用于生成轮廓的数据。
链接到数据:https://github.com/jkiefn1/Contours_and_plotly
创建Map框:
# Create the static figure
fig = px.density_mapbox(df
,lat='lat'
,lon='long'
,z='Rand_Data'
,hover_data={
'lat':True # remove from hover data
,'long':True # remove from hover data
,col:True
}
,center=dict(lat=38.5, lon=-96)
,zoom=3
,radius=30
,opacity=0.5
,mapbox_style='open-street-map'
,color_continuous_scale='inferno'
)
fig.show()
创建matplotlib等高线图,生成geojson文件
# Load in the DataFrame
path = r'/Users/joe_kiefner/Desktop/Sample_Data.csv'
df = pd.read_csv(path, index_col=[0])
data = []
# Define rain levels to be contours in geojson
levels = [0.25,0.5,1,2.5,5,10]
colors = ['royalblue', 'cyan', 'lime', 'yellow', 'red']
vmin = 0
vmax = 1
cm = branca.colormap.LinearColormap(colors, vmin=vmin, vmax=vmax).to_step(len(levels))
x_orig = (df.long.values.tolist())
y_orig = (df.lat.values.tolist())
z_orig = np.asarray(df['Rain_in'].values.tolist())
x_arr = np.linspace(np.min(x_orig), np.max(x_orig), 500)
y_arr = np.linspace(np.min(y_orig), np.max(y_orig), 500)
x_mesh, y_mesh = np.meshgrid(x_arr, y_arr)
xscale = df.long.max() - df.long.min()
yscale = df.lat.max() - df.lat.min()
scale = np.array([xscale, yscale])
z_mesh = griddata((x_orig, y_orig), z_orig, (x_mesh, y_mesh), method='linear')
sigma = [5, 5]
z_mesh = sp.ndimage.filters.gaussian_filter(z_mesh, sigma, mode='nearest')
# Create the contour
contourf = plt.contourf(x_mesh, y_mesh, z_mesh, levels, alpha=0.9, colors=colors,
linestyles='none', vmin=vmin, vmax=vmax)
# Convert matplotlib contourf to geojson
geojson = geojsoncontour.contourf_to_geojson(
contourf=contourf,
min_angle_deg=3,
ndigits=2,
unit='in',
stroke_width=1,
fill_opacity=0.3)
d = json.loads(geojson)
len_features=len(d['features'])
if not data:
data.append(d)
else:
for i in range(len(d['features'])):
data[0]['features'].append(d['features'][i])
with open('/path/to/Sample.geojson', 'w') as f:
dump(geojson, f)
1条答案
按热度按时间1dkrff031#
1.有两个核心选项
1.添加为层https://plotly.com/python/mapbox-layers/
1.添加为choropleth迹线https://plotly.com/python/mapbox-county-choropleth/
1.layers-legend-与layers选项相同,但通过向图中添加额外的轨迹来创建图例
1.这两个选项都被编码在下面.改变
OPTION
的值以在它们之间切换1.layers表示没有图例或悬停文本
1.choropleth这些是存在的,移动colorbar,使其不重叠图例。更多的图例和悬停文本的美化需要...
层
跟踪