英文:
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(...))
的快捷方式。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论