使用Plotly和Choropleth绘制多边形。

huangapple go评论62阅读模式
英文:

Draw polygons with Plotly and Choropleth

问题

我想绘制具有不同颜色的大量多边形。

使用add_shapes生成的图表速度太慢。

我怀疑使用choropleth应该足够好。目前,我有以下代码:

import plotly.express as px

polygons = [[(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)],
            [(3, 3), (3, 4), (4, 4), (4, 3), (3, 3)],
            [(5, 5), (5, 6), (6, 6), (6, 5), (5, 5)],
            [(7, 7), (7, 8), (8, 8), (8, 7), (7, 7)]]

geojson = {'features': [], 'type': 'FeatureCollection'}
ids = []
for index, poly in enumerate(polygons):
    temp_dict = {
        'geometry': {
            'coordinates': [polygons[index].copy()],
            'type': 'Polygon'},
        'type': 'Feature',
        'id': index}
    geojson['features'].append(temp_dict)
    ids.append(index)

fig = px.choropleth(
        geojson=geojson,
        color=ids,
        locations=ids)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.show()

它似乎可以工作,但我想删除背景中的地图,因为我的多边形与地理位置无关。有没有办法做到这一点或获得等效的结果?

英文:

I want to draw a high number of polygons with different colors.

Using add_shapes generates plots which are way to slow.

I suspect that using choropleth would work well enough. Currently, I have the following code:

import plotly.express as px

polygons = [[(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)],
            [(3, 3), (3, 4), (4, 4), (4, 3), (3, 3)],
            [(5, 5), (5, 6), (6, 6), (6, 5), (5, 5)],
            [(7, 7), (7, 8), (8, 8), (8, 7), (7, 7)]]

geojson = {'features': [], 'type': 'FeatureCollection'}
ids = []
for index, poly in enumerate(polygons):
    temp_dict = {
        'geometry': {
            'coordinates': [polygons[index].copy()],
            'type': 'Polygon'},
        'type': 'Feature',
        'id': index}
    geojson['features'].append(temp_dict)
    ids.append(index)

fig = px.choropleth(
        geojson=geojson,
        color=ids,
        locations=ids)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.show()

It seems to work, but I'd like to remove the map in the background, since my polygons are not related to geographic locations. Is there a way to do it or to get an equivalent result?

答案1

得分: 2

使用分级色块多边形是一个很好的想法,你只需要一件事来摆脱基础地图

fig.update_geos(visible=False)

注意:fig.update_geo(...)fig.update_layout(geo=dict(...)) 的快捷方式。

另请参阅Python图表参考:layout.geo

英文:

Using choropleth polygons is a great idea, you just need one thing to get rid of the base map :

fig.update_geos(visible=False)

NB. fig.update_geo(...) is a shortcut for fig.update_layout(geo=dict(...)).

@see also Python Figure Reference: layout.geo

huangapple
  • 本文由 发表于 2023年7月20日 21:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定