1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        如何用Python畫出好看的地圖

        共 4798字,需瀏覽 10分鐘

         ·

        2020-11-20 22:23


        導(dǎo)讀:最近正好在學(xué)空間數(shù)據(jù)處理,本文就探討一下用Python如何畫出好看的地圖。


        作者:小周
        來源:小周note(ID:XZnote)


        下面主要是用

        • folium
        • Plotly
        • GeoPandas+Matplotlib

        三種方式繪制地圖。


        01 folium


        1import?folium
        2import?pandas?as?pd
        3#輸入上海經(jīng)緯度,尺度
        4latitude?=?31.2
        5longitude?=?121.5
        6sh_map?=?folium.Map(location=[latitude,?longitude],?zoom_start=10)
        7sh_map


        默認(rèn)為'OpenStreetMap'風(fēng)格,我們還可以選擇'Stamen Terrain', 'Stamen Toner'等。

        1sh_map?=?folium.Map(location=[latitude,?longitude],?zoom_start=10,tiles='Stamen?Toner')
        2sh_map


        有了底圖把帶有經(jīng)緯度的數(shù)據(jù)點(diǎn)映射上去,這里用上次上海poi中的火鍋數(shù)據(jù):


        把火鍋店數(shù)據(jù)放到地圖上:

         1#?創(chuàng)建特征組
        2hotpots?=?folium.map.FeatureGroup()
        3#?Loop?through?the?200?crimes?and?add?each?to?the?incidents?feature?group
        4for?lat,?lng,?in?zip(data.lat,?data.lon):
        5????hotpots.add_child(
        6????????folium.CircleMarker(
        7????????????[lat,?lng],
        8????????????radius=7,?#?define?how?big?you?want?the?circle?markers?to?be
        9????????????color='yellow',
        10????????????fill=True,
        11????????????fill_color='red',
        12????????????fill_opacity=0.4
        13????????)
        14????)
        15
        16#?把火鍋特征組放到上海地圖上
        17sh_map?=folium.Map(location=[latitude,?longitude],?zoom_start=10)
        18sh_map.add_child(hotpots)


        點(diǎn)太多看不出什么,我們下面只放嘉定區(qū)的數(shù)據(jù):


        我們可以繼續(xù)添加火鍋店名字信息并標(biāo)注在上圖:

        1latitudes?=?list(datas.lat)
        2longitudes?=?list(datas.lon)
        3labels?=?list(datas.name)
        4for?lat,?lng,?label?in?zip(latitudes,?longitudes,?labels):
        5????folium.Marker([lat,?lng],?popup=label).add_to(sh_map)??????
        6sh_map


        最后我們繪制每個(gè)區(qū)的火鍋數(shù)量的Choropleth Map,不同顏色代表火鍋數(shù)量。

        首先讀取上海行政區(qū)geojson文件:


        統(tǒng)計(jì)各個(gè)區(qū)火鍋數(shù)量:

        1hots=data.groupby('adname',as_index=False).agg({'name':"count"})
        2hots.columns=['district','num']
        3hots


        開始繪制Choropleth Map,下面key_on='feature.properties.name'是因?yàn)槲覀冞@里下載的上海geojson中name對(duì)應(yīng)于行政區(qū),這個(gè)根據(jù)具體數(shù)據(jù)而定。


         1#輸入上海經(jīng)緯度,尺度
        2latitude?=?31.2
        3longitude?=?121.5
        4map?=?folium.Map(location=[latitude,?longitude],?zoom_start=12)
        5folium.Choropleth(
        6????geo_data=geo_data,
        7????data=hots,
        8????columns=['district','num'],
        9????key_on='feature.properties.name',
        10????#fill_color='red',
        11????fill_color='YlOrRd',
        12????fill_opacity=0.7,
        13????line_opacity=0.2,
        14????highlight=True,
        15????legend_name='Hotpot?Counts?in?Shanghai'
        16).add_to(map)
        17map

        ▲浦東新區(qū)一馬當(dāng)先,嘉定區(qū)榜上有名


        02 Plotly


        用Plotly畫Choropleth Map跟folium很類似,這里直接繼續(xù)用前面處理好的數(shù)據(jù)。

         1import?plotly.express?as?px
        2import?plotly.graph_objs?as?go
        3latitude?=?31.2
        4longitude?=?121.5
        5fig?=?px.choropleth_mapbox(
        6????data_frame=hots,
        7????geojson=geo_data,
        8????color='num',
        9????locations="district",
        10????featureidkey="properties.name",
        11????mapbox_style="carto-positron",
        12????color_continuous_scale='viridis',
        13????center={"lat":?latitude,?"lon":?longitude},
        14????zoom=7.5,
        15)
        16fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
        17fig.show()


        設(shè)置mapbox_style="carto-darkmatter"換一種風(fēng)格。


        更多用Plotly畫圖的例子見
        https://plotly.com/python/



        03 GeoPandas+Matplotlib


        先利用GeoPandas繪制出各行政區(qū)輪廓:

         1import?matplotlib.pyplot?as?plt
        2import?matplotlib?as?mpl
        3#?中文和負(fù)號(hào)的正常顯示
        4mpl.rcParams['font.sans-serif']?=?['Times?New?Roman']
        5mpl.rcParams['font.sans-serif']?=?[u'SimHei']
        6mpl.rcParams['axes.unicode_minus']?=?False
        7#?裁剪上海shape
        8fig,?ax?=?plt.subplots(figsize=(20,?10))
        9ax=geo_data.plot(ax=ax,facecolor='grey',edgecolor='white',linestyle='--',alpha=0.8)
        10ax.axis('off')?#?移除坐標(biāo)軸


        將統(tǒng)計(jì)的各個(gè)區(qū)的火鍋店數(shù)量與geo_data合并:

        1need_data=pd.merge(geo_data,hots[['district','num']],left_on='name',
        2?????????????????right_on='district')
        3need_data


        此時(shí)數(shù)據(jù)已經(jīng)是dataframe,不是geodataframe了,所以需要繼續(xù)轉(zhuǎn)換一下。

        1need_data?=?gpd.GeoDataFrame(need_data)
        2need_data.geom_type


        可以看到這個(gè)時(shí)候need_data已經(jīng)是geodataframe類型了。

        1fig,?ax?=?plt.subplots(figsize=(20,?10))
        2need_data.plot('num',?cmap='OrRd',ax=ax)
        3ax.axis('off')?#?移除坐標(biāo)軸
        4cmap?=?mpl.cm.get_cmap('OrRd')
        5norm?=?mpl.colors.Normalize(min(need_data['num']),?max(need_data['num']))
        6fcb?=?fig.colorbar(mpl.cm.ScalarMappable(norm=norm,?cmap=cmap),ax=ax)
        7ax=geo_data.plot(ax=ax,facecolor='grey',edgecolor='white',linestyle='--',alpha=0.2)
        8ax.set_title('Hotpot?Count?in?Shanghai',?fontsize=20)


        不熟悉上海的人是看不出來各個(gè)區(qū)的,這里可以繼續(xù)加行政區(qū)標(biāo)注:

         1fig,?ax?=?plt.subplots(figsize=(20,?10))
        2need_data.plot('num',?cmap='OrRd',ax=ax)
        3for?idx,?_?in?enumerate(need_data.geometry.representative_point()):
        4????#?提取行政區(qū)名稱
        5????region?=?need_data.loc[idx,?'name']
        6????ax.text(_.x,?_.y,?region,?ha="center",?va="center",?size=8)
        7ax.axis('off')?#?移除坐標(biāo)軸
        8cmap?=?mpl.cm.get_cmap('OrRd')
        9norm?=?mpl.colors.Normalize(min(need_data['num']),?max(need_data['num']))
        10fcb?=?fig.colorbar(mpl.cm.ScalarMappable(norm=norm,?cmap=cmap),ax=ax)
        11ax=geo_data.plot(ax=ax,facecolor='grey',edgecolor='white',linestyle='--',alpha=0.2)
        12ax.set_title('Hotpot?Count?in?Shanghai',?fontsize=20)



        本文的notebook及數(shù)據(jù)可在公眾號(hào)后臺(tái)對(duì)話框回復(fù)源代碼獲取。


        劃重點(diǎn)?


        干貨直達(dá)?


        更多精彩?

        在公眾號(hào)對(duì)話框輸入以下關(guān)鍵詞
        查看更多優(yōu)質(zhì)內(nèi)容!

        PPT?|?讀書?|?書單?|?硬核?|?干貨?|?講明白?|?神操作
        大數(shù)據(jù)?|?云計(jì)算?|?數(shù)據(jù)庫?|?Python?|?可視化
        AI?|?人工智能?|?機(jī)器學(xué)習(xí)?|?深度學(xué)習(xí)?|?NLP
        5G?|?中臺(tái)?|?用戶畫像?|?1024?|?數(shù)學(xué)?|?算法?|?數(shù)字孿生

        據(jù)統(tǒng)計(jì),99%的大咖都完成了這個(gè)神操作
        ?


        瀏覽 87
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            大波大乳巨大pics | 爱搞免费视频 | 国产一级A片久久久免费看快餐 | 一级黄色录像影片夫妻性生活影片 | 俺也去俺去也在线观看 | 香蕉av影院 | 婷婷久久五月天 | 色欲无码婬片A片视频 | 欧美一二| 老司机一级毛片 |