我想绘制大量不同颜色的多边形。
使用
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()
它似乎有效,但我想删除后台的地图,因为我的多边形与地理位置无关。有没有办法做到或得到等效的结果?
使用等值线多边形是个好主意,您只需要一件事即可摆脱底图:
fig.update_geos(visible=False)
注意。
fig.update_geos(...)
是 fig.update_layout(geo=dict(...))
的快捷方式。
@另请参阅 Python 图参考:
layout.geo