简体   繁体   English

如何在 Python / Plotly choropleth plot 中放大地理 map?

[英]How to enlarge geographic map in Python/Plotly choropleth plot?

I am making a choropleth plot with Plotly.我正在用 Plotly 制作合唱 plot。 But, the geographic map looking to small.但是,地理 map 看起来很小。

like that:像那样:

My code is here:我的代码在这里:

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))

width and height attributes are not making exactly what i want to do.宽度和高度属性并没有完全符合我的要求。 I want to enlarge only geographic map.我只想放大地理 map。 How can i do that in plotly?我怎么能在 plotly 中做到这一点?

I had the same problem.我有同样的问题。 I found this related issue , which says:我发现了这个相关的问题,它说:

Plotly tries to take all the available space without changing the image ratio. Plotly 尝试在不更改图像比例的情况下占用所有可用空间。 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.如果您有一个非常宽的 div,则由于左右两侧会有很多空白空间,但它将从顶部到底部填充。

I increased the height of my layout.我增加了布局的height I went from this:我从这个出发:

fig.update_layout(margin=dict(l=0, r=0, b=0, t=0),
                  width=1500)

to this:对此:

fig.update_layout(margin=dict(l=0, r=0, b=0, t=0),
                  width=1500, 
                  height=800)

which improved my image size dramatically.这极大地改善了我的图像大小。

This still left considerable whitespace around my map.这仍然在我的 map 周围留下了相当大的空白。 I improved this by computing the bounding box instead of using fitbounds .我通过计算边界框而不是使用fitbounds来改进这一点。 I used a helper module turfpy to compute the bounding box of a geometry, installed using pip install turfpy .我使用辅助模块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,
)

Take a look at Plotly's website and scroll down to the fitbounds section.看看Plotly 的网站并向下滚动到 fitbounds 部分。

I believe your problem may be the code fig.update_geos(fitbounds="locations", visible=False)我相信你的问题可能是代码fig.update_geos(fitbounds="locations", visible=False)

where you need to change the section "locations" to something else such as "geojson" or "False".您需要将“位置”部分更改为其他内容,例如“geojson”或“False”。

Hope this works!希望这有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM