我正在使用 Plotly 制作等值线图。但是,地理 map 看起来很小。
我的代码在这里:
fig = px.choropleth(df,
geojson=geojson,
locations="Capitalize",
featureidkey="properties.name",
color="Scale",
hover_name='Capitalize',
hover_data=['Quantity'],
animation_frame="Period",
projection="mercator",)
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(title_text = 'Product A',
autosize=True,
hovermode='closest',
title_x = 0.5,
margin={"r":0,"t":100,"l":0,"b":0},
geo=dict(showframe = False, showcoastlines = False))
宽度和高度属性不完全符合我的要求。我只想放大 map 。我怎样才能巧妙地做到这一点?
最佳答案
我遇到了同样的问题。我找到了这个 related issue ,它说:
Plotly tries to take all the available space without changing the image ratio. If you have a very wide div there will be a lot of empty space to left and right due but it will be filled from the top to the bottom.
我增加了布局的高度
。我从这里开始:
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0),
width=1500)
为此:
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0),
width=1500,
height=800)
这极大地改善了我的图像大小。
这仍然在我的 map 周围留下了相当大的空白。我通过计算边界框而不是使用 fitbounds
改进了这一点。我使用了辅助模块 turfpy
来计算几何体的边界框,使用 pip install turfpy
安装。
from turfpy.measurement import bbox
from functools import reduce
def compute_bbox(gj):
gj_bbox_list = list(
map(lambda f: bbox(f['geometry']), gj['features']))
gj_bbox = reduce(
lambda b1, b2: [min(b1[0], b2[0]), min(b1[1], b2[1]),
max(b1[2], b2[2]), max(b1[3], b2[3])],
gj_bbox_list)
return gj_bbox
gj_bbox = compute_bbox(gj)
fig = px.choropleth(fdf,
geojson=gj,
locations=locationcol,
color=datacol,
color_continuous_scale="Viridis",
range_color=(0, max_value),
featureidkey=key,
scope='europe',
hover_data=[namecol, 'LAD11NM']
)
fig.update_geos(
# fitbounds="locations",
center_lon=(gj_bbox[0]+gj_bbox[2])/2.0,
center_lat=(gj_bbox[1]+gj_bbox[3])/2.0,
lonaxis_range=[gj_bbox[0], gj_bbox[2]],
lataxis_range=[gj_bbox[1], gj_bbox[3]],
visible=False,
)
关于python - 如何在 Python/Plotly 等值线图中放大 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466163/