简体   繁体   English

西班牙 Map 与 plotly python

[英]Map of Spain with plotly python

I was looking for a map of Spain by regions with plotly .我正在寻找西班牙的 map 地区 plotly Has anyone done it in the past?过去有人做过吗? I am interested in knowing the code or the way to proceed to be able to do it, I see little information on the internet.我有兴趣了解代码或继续执行此操作的方法,我在互联网上看到的信息很少。

import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_geos(
    visible=False, resolution=50, scope="Spain",
    showcountries=True, countrycolor="Black",
    showsubunits=True, subunitcolor="Blue"
)
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

I have the previous code, but "spain" is not defined in the scope, "europe", "usa", and others there are, so, how can I change it to make Spain with plotly?我有以前的代码,但是scope 中没有定义“西班牙”, “欧洲”,“美国”等有,那么,我怎样才能将其更改为使用 plotly 制作西班牙?

  • using mapbox instead of geos使用mapbox而不是geos
  • you can add layers to mapbox scatter您可以将图层添加到mapbox scatter
  • have sourced cities and boundary geometries to demonstrate已经采购了城市和边界几何图形来展示
import requests
import plotly.express as px
import pandas as pd

# get Spain municipal boundaries
res = requests.get(
    "https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/spain-provinces.geojson"
)

# get some cities in Spain
df = (
    pd.json_normalize(
        requests.get(
            "https://opendata.arcgis.com/datasets/6996f03a1b364dbab4008d99380370ed_0.geojson"
        ).json()["features"]
    )
    .loc[
        lambda d: d["properties.CNTRY_NAME"].eq("Spain"),
        ["properties.CITY_NAME", "geometry.coordinates"],
    ]
    .assign(
        lon=lambda d: d["geometry.coordinates"].apply(lambda v: v[0]),
        lat=lambda d: d["geometry.coordinates"].apply(lambda v: v[1]),
    )
)

# scatter the cities and add layer that shows municiple boundary
px.scatter_mapbox(df, lat="lat", lon="lon", hover_name="properties.CITY_NAME").update_layout(
    mapbox={
        "style": "carto-positron",
        "zoom": 3.5,
        "layers": [
            {
                "source": res.json(),
                "type": "line",
                "color": "green",
                "line": {"width": 1},
            }
        ],
    }
)

在此处输入图像描述

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

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