[英]How to enlarge geographic map in Python/Plotly choropleth plot?
我正在用 Plotly 制作合唱 plot。 但是,地理 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。 我怎么能在 plotly 中做到這一點?
我有同樣的問題。 我發現了這個相關的問題,它說:
Plotly 嘗試在不更改圖像比例的情況下占用所有可用空間。 如果您有一個非常寬的 div,則由於左右兩側會有很多空白空間,但它將從頂部到底部填充。
我增加了布局的height
。 我從這個出發:
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,
)
看看Plotly 的網站並向下滾動到 fitbounds 部分。
我相信你的問題可能是代碼fig.update_geos(fitbounds="locations", visible=False)
您需要將“位置”部分更改為其他內容,例如“geojson”或“False”。
希望這有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.