python筆記:5.3.19_基本統計圖形_數據地圖

    這個plotly實現的數據地圖比較不爽的是,完成後數據自動上傳,並需要在瀏覽器中才能看。

# -*- coding: utf-8 -*-
"""
Created on Mon Jun 10 14:04:49 2019

@author: User
"""

import plotly as pl
import plotly.plotly as py
import numpy as np
import pandas as pd

pl.tools.set_credentials_file(username='***',api_key='***')

df=pd.read_csv(u'data\\ch5\chinacitypop.csv',encoding = "gbk")
print(df.info())

df['text']=df['name'] + '<br>Population'+(df['pop']).astype(str)+' ten thousand'
limits=[(0,2),(3,10),(11,100),(101,200),(201,350)]
colors=['rgb(0,116,217)',
        'rgb(255,65,54)',
        'rgb(133,20,75)',
        'rgb(255,133,27)',
        'rgb(155,133,27)']
cities=[]
scale=10

for i in range(len(limits)):
    lim=limits[i]
    df_sub=df[lim[0]:lim[1]]
    city=dict(type='scattergeo',locationmode='USA-states',lon=df_sub['lon'],
              lat=df_sub['lat'],text=df_sub['text'],
              marker=dict(size=df_sub['pop']/scale,color=colors[i],
                          line=dict(width=0.5,color='rgb(40,40,40)'),
                          sizemode='area'),name='{0}-{1}'.format(lim[0],lim[1]))
    cities.append(city)

layout=dict(title='2014 China city populations<br>(Click legend to toggle traces)',
            showlegend=True,
            geo=dict(scope='asia',projection=dict(type='mercator'),
                     showland=True,
                     landcolor='rgb(217,217,217)',subunitwidth=1,
                     countrywidth=1,
                     subunitcolor='rgb(255,255,255)',
                     countrycolor='rgb(255,255,255)'),)

fig=dict(data=cities,layout=layout)
py.iplot(fig,filename='d3-bubble-map-chn-populations')

運行:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章