weixin_52849274
weixin_52849274
2021-06-22 15:48
采纳率: 100%
浏览 83

这个还能添加一些别的功能吗?求添加一段二三十行左右的代码

import plotly.express as px

import plotly.graph_objects as go

import requests

import json

import pandas as pd

 

 

data = pd.read_excel('房价汇总带经纬度.xlsx')

token = 'pk.eyJ1IjoibWFyb2NvNjY2IiwiYSI6ImNrcHhmeDlrYjBmNHUycG9ibDU5ZjRzd2oifQ.2HDoU4mAkfpKts0YZtEmyA'

fig = go.Figure(go.Scattermapbox(

    mode="markers",

    lon=data.经度,

    lat=data.纬度,

    hovertext=data.简介, # 悬停显示文本内容

    hoverinfo='text',

    marker_symbol='marker',

    marker_size=6)) # 标记大小

fig.update_layout(mapbox={

    "accesstoken": token,

    "center": {'lon': 116.2, 'lat': 39.56}, # 地图中心位置

    'zoom': 11.8},

    margin={'l': 0, 'r': 0, 't': 0, "b": 0} # 地图边距

)

 

 

fig = px.scatter_mapbox(data,

                        lon='经度', # 输入经度坐标

                        lat='纬度', # 输入纬度

                        color="单价", # 对应excel的color栏,每个值代表一种颜色

                        hover_name="简介", # 可以对应excel里面的某一栏

                        size_max=16, # 上面size尺寸最大值

                        color_continuous_scale=px.colors.carto.Temps

                        )

fig.update_layout(mapbox={'accesstoken': token,

                          "center": {'lon': 116.2, 'lat': 39.56}, # 指定的地图中心

                          'zoom': 7.48,

                          'style': 'dark', # 显示的地图类型

                          },

                  margin={'l': 0, 'r': 0, 't': 0, 'b': 0})

fig.show() # 显示地图

 

 

url = 'http://map.amap.com/service/subway?_1599997789354=&srhdata=1100_drw_beijing.json'

response = requests.get(url)

result = json.loads(response.text)

stations = []

lats = []

lons = []

lines = []

for i in result['l']:

    for j in i['st']:

        lines.append(i['kn'])

        stations.append(j['n'])

        lons.append(j['sl'].split(',')[0])

        lats.append(j['sl'].split(',')[1])

dataframe = pd.DataFrame({'站名': stations, '线路': lines, '经度': lons, '纬度': lats})

 

lines = dataframe['线路'].unique().tolist()

for line in lines:

    fig.add_traces(go.Scattermapbox(

        mode="markers+lines",

        lon=dataframe.loc[lambda x: x['线路'] == line]['经度'],

        lat=dataframe.loc[lambda x: x['线路'] == line]['纬度'],

        hovertext=dataframe.loc[lambda x: x['线路'] == line]['站名'],

        hoverinfo='text',

        marker_symbol={'a': 'b'},

        marker_size={1: 2},

        showlegend=False))

for line in lines[1:]:

    fig.add_traces(go.Scattermapbox(

        mode="markers+lines",

        lon=dataframe.loc[lambda x: x['线路'] == line]['经度'],

        lat=dataframe.loc[lambda x: x['线路'] == line]['纬度'],

        hovertext=dataframe.loc[lambda x: x['线路'] == line]['站名'],

        hoverinfo='text',

        marker_symbol={'a': 'b'},

        marker_size={1: 2},

        showlegend=False))

fig.update_layout(mapbox={"accesstoken": token, "center": {'lon': 116.2, 'lat': 39.56}, 'zoom': 11.8},

                  margin={'l': 0, 'r': 0, 't': 0, "b": 0})

  • 点赞
  • 收藏

3条回答 默认 最新

  • Feng_wwf
    NDSC专家-王文峯 2021-06-22 16:01
    已采纳
    import plotly.express as px
    import plotly.graph_objects as go
    import requests
    import json
    import pandas as pd
    data = pd.read_excel('房价汇总带经纬度.xlsx')
    token =
        'pk.eyJ1IjoibWFyb2NvNjY2IiwiYSI6ImNrcHhmeDlrYjBmNHUycG9ibDU5ZjRzd2oifQ.2HDoU4mAkfpKts0YZtEmyA'
    fig = go.Figure(go.Scattermapbox(
        mode = "markers",
        lon = data.经度,
        lat = data.纬度,
        hovertext = data.简介, #悬停显示文本内容
        hoverinfo = 'text',
        marker_symbol = 'marker',
        marker_size = 6))# 标记大小
    fig.update_layout(mapbox = {
            "accesstoken": token,
            "center":
            {
                'lon': 116.2,
                'lat': 39.56
            },
            #地图中心位置
            'zoom': 11.8
        },
        margin = {
            'l': 0,
            'r': 0,
            't': 0,
            "b": 0
        }#
        地图边距
    )
    fig = px.scatter_mapbox(data,
        lon = '经度', #输入经度坐标
        lat = '纬度', #输入纬度
        color = "单价", #对应excel的color栏, 每个值代表一种颜色
        hover_name = "简介", #可以对应excel里面的某一栏
        size_max = 16, #上面size尺寸最大值
        color_continuous_scale = px.colors.carto.Temps
    )
    fig.update_layout(mapbox = {
            'accesstoken': token,
            "center":
            {
                'lon': 116.2,
                'lat': 39.56
            },
            #指定的地图中心
            'zoom': 7.48,
            'style': 'dark',
            #显示的地图类型
        },
        margin = {
            'l': 0,
            'r': 0,
            't': 0,
            'b': 0
        })
    fig.show()# 显示地图
    url =
        'http://map.amap.com/service/subway?_1599997789354=&srhdata=1100_drw_beijing.json'
    response = requests.get(url)
    result = json.loads(response.text)
    stations = []
    lats = []
    lons = []
    lines = []
    for i in result['l']:
        for j in i['st']:
        lines.append(i['kn'])
    stations.append(j['n'])
    lons.append(j['sl'].split(',')[0])
    lats.append(j['sl'].split(',')[1])
    dataframe = pd.DataFrame(
    {
        '站名': stations,
        '线路': lines,
        '经度': lons,
        '纬度': lats
    })
    lines = dataframe['线路'].unique().tolist()
    for line in lines:
        fig.add_traces(go.Scattermapbox(
            mode = "markers+lines",
            lon = dataframe.loc[lambda x: x['线路'] == line]['经度'],
            lat = dataframe.loc[lambda x: x['线路'] == line]['纬度'],
            hovertext = dataframe.loc[lambda x: x['线路'] == line]['站名'],
            hoverinfo = 'text',
            marker_symbol = {
                'a': 'b'
            },
            marker_size = {
                1: 2
            },
            showlegend = False))
    for line in lines[1: ]:
        fig.add_traces(go.Scattermapbox(
            mode = "markers+lines",
            lon = dataframe.loc[lambda x: x['线路'] == line]['经度'],
            lat = dataframe.loc[lambda x: x['线路'] == line]['纬度'],
            hovertext = dataframe.loc[lambda x: x['线路'] == line]['站名'],
            hoverinfo = 'text',
            marker_symbol = {
                'a': 'b'
            },
            marker_size = {
                1: 2
            },
            showlegend = False))
    fig.update_layout(mapbox = {
            "accesstoken": token,
            "center":
            {
                'lon': 116.2,
                'lat': 39.56
            },
            'zoom': 11.8
        },
        margin = {
            'l': 0,
            'r': 0,
            't': 0,
            "b": 0
        })

    建议如下:如有帮助,请采纳一下,谢谢

    点赞 1 评论
  • robot8me
    robot8me 2021-06-22 16:00

    你这个是要做什么?感觉你是把两个代码直接放到了一起,一个是房价和经纬度做了图形显示,一个整了铁路的线路站名经纬度啥的。铁路这个还需要申请接口权限吧

    点赞 1 评论
  • Feng_wwf
    NDSC专家-王文峯 2021-06-23 16:21

     

    url =
        'http://map.amap.com/service/subway?_1599997789354=&srhdata=1100_drw_beijing.json' #获取地图的api
    response = requests.get(url) #获取url
    result = json.loads(response.text) #加载数据
    stations = [] #定义站名
    lats = [] #纬度
    lons = []#经度
    lines = [] # 线路
    for i in result['l']:
        for j in i['st']:#遍历
        lines.append(i['kn']) #追加
    stations.append(j['n'])
    lons.append(j['sl'].split(',')[0])
    lats.append(j['sl'].split(',')[1]) 
    dataframe = pd.DataFrame( #数据列
    {
        '站名': stations,
        '线路': lines,
        '经度': lons,
        '纬度': lats
    })
    lines = dataframe['线路'].unique().tolist()
    for line in lines:
        fig.add_traces(go.Scattermapbox(
            mode = "markers+lines",
            lon = dataframe.loc[lambda x: x['线路'] == line]['经度'],
            lat = dataframe.loc[lambda x: x['线路'] == line]['纬度'],
            hovertext = dataframe.loc[lambda x: x['线路'] == line]['站名'],
            hoverinfo = 'text',
            marker_symbol = {
                'a': 'b'
            },
            marker_size = {
                1: 2
            },
            showlegend = False))
    for line in lines[1: ]:
        fig.add_traces(go.Scattermapbox(
            mode = "markers+lines",
            lon = dataframe.loc[lambda x: x['线路'] == line]['经度'],
            lat = dataframe.loc[lambda x: x['线路'] == line]['纬度'],
            hovertext = dataframe.loc[lambda x: x['线路'] == line]['站名'],
            hoverinfo = 'text',
            marker_symbol = {
                'a': 'b'
            },
            marker_size = {
                1: 2
            },
            showlegend = False))
    fig.update_layout(mapbox = {
            "accesstoken": token,
            "center":
            {
                'lon': 116.2,
                'lat': 39.56
            },
            'zoom': 11.8
        },
        margin = {
            'l': 0,
            'r': 0,
            't': 0,
            "b": 0
        })
    点赞 评论

相关推荐