如何使用Plotly在Python中為任何DataFrame繪制地圖的衛星視圖

Chart-Studio和Mapbox簡介 (Introduction to Chart-Studio and Mapbox)

Folium and Geemap are arguably the best GIS libraries/tools to plot satellite-view maps or any other kinds out there, but at times they require an additional authorization to use the Google Earth Engine, which is not accessible to many people.

FoliumGeemap可以說是繪制衛星視圖地圖或其他任何種類的最佳GIS庫/工具,但是有時它們需要使用Google Earth Engine的額外授權,但很多人無法使用。

Using Chart-Studio and Mapbox is a simple alternative to this, and to be fair they produce amazing results as well. Chart Studio is a package that contains utilities for interfacing with Plotly’s Chart Studio service (both Chart Studio cloud and Chart Studio On-Prem) — which is extensively used in creating interactive plots online. Mapbox is a location data platform for mobile and web applications, which we are going to be using to help craft our map.

使用Chart-StudioMapbox是一種簡單的替代方法,公平地講,它們也會產生驚人的結果。 Chart Studio是一個軟件包,其中包含用于與Plotly的Chart Studio服務( Chart Studio云和Chart Studio On-Prem )接口的實用程序,該服務廣泛用于在線創建交互式繪圖。 Mapbox是用于移動和Web應用程序的位置數據平臺,我們將使用它來幫助制作地圖。

An additional library which is required, and is used throughout this tutorial is chart_studio. This library can easily be installed in the command line or in the anaconda prompt.

在本教程中使用的另一個必需庫是chart_studio 。 該庫可以輕松地安裝在命令行或anaconda提示符中。

其他要求 (Additional Requirements)

Other than having the necessary libraries, to implement this code, you should have accounts created in both Mapbox (can be created here), and in Plotly Chart Studio (can be created here). Needless to say both are free to use.

除了擁有必要的庫之外,要實現此代碼,您還應該在Mapbox (可以在此處創建)和Plotly Chart Studio (可以在此處創建)中創建帳戶 。 不用說兩者都可以免費使用。

Now everything’s ready, and we’re all set to go.

現在一切準備就緒,我們已經準備就緒。

1.重塑DataFrame (1. Reshaping the DataFrame)

The DataSets we start off with, in our projects or web apps usually look like this :

我們在項目或Web應用程序中開始使用的數據集通常如下所示:

John Hopkins COVID -19 repositoryJohn Hopkins COVID -19信息庫

These tables will not pose a problem if you need to plot data from just one column. But at times we need to plot maps for data from more than one column , or a combination of them (say in this example- “confirmed”, “deaths”, and “recovered”). Unpivoting this table, i.e, mapping from one to many, allows simple iterative statements to help us compile data from each column.

如果您只需要從一列中繪制數據,這些表就不會造成問題。 但是有時我們需要為多于一列的數據或它們的組合繪制地圖(在此示例中為“已確認”,“死亡”和“已恢復”)。 取消透視表(即從一個表映射到多個表),可以使用簡單的迭代語句來幫助我們編譯每一列中的數據。

So we convert it to something like this :

因此,我們將其轉換為如下形式:

Required form of the DataFrame above
上面的DataFrame的必需形式

If you are already familiar with the pandas library, you can do this yourself by calling the df.stack() method or pandas.melt() function, specific to your table and skip this step. If you aren’t too familiar with it though, or want a quick solution without having to go through the pandas docs , that’s all right. As long you have the required columns, I’ll give you a function that does it for you :).

如果您已經熟悉pandas庫,則可以通過調用特定于表的df.stack()方法或pandas.melt()函數來自己完成此操作,并跳過此步驟。 如果您不太熟悉它,或者想要快速解決方案而不必閱讀pandas docs ,那就可以了。 只要您具有必填列,我就會為您提供一個為您完成此功能的函數:)。

All you need are the following columns:

您只需要以下幾欄:

  1. A location column- containing the country name/district/state, etc. which will be used for naming the subtraces,

    位置列-包含國家名稱/地區/州等,將用于命名子跡線,
  2. A list of columns whose data you would like to plot on your map(one or more than one),

    您想在地圖上繪制其數據的列的列表(一個或多個),
  3. A column containing the latitude of these locations,

    一列包含這些位置的緯度,
  4. And a column containing the longitude of these locations

    還有一列包含這些位置的經度

Feed the DataSet and these columns into the following function, and you will have your formatted DataFrame. Let’s move on.

將DataSet和這些列輸入以下函數,您將獲得格式化的DataFrame。 讓我們繼續。

cols parameter takes in a list in the following format [‘location column’, [*’columns with data to be plotted’], ‘column with latitude’, ‘column with longitude’]
cols參數采用以下格式的列表:['位置列',[*'要繪制數據的列'],'緯度列','經度列']

2.創建數據/痕跡 (2. Creating the Data/Trace)

Now that our DataFrame is ready, we need create the trace for our plot. Choosing any one object of study from our formatted DataFrame, the following function creates the ‘Data’ (or trace)for this study, and returns it. It’s as simple as that. (Note that I used the math.log() function to get a scaled size, but if your values are very close or if it includes negative counts, you should probably change that.)

現在我們的DataFrame已經準備好了,我們需要為繪圖創建軌跡。 從我們格式化的DataFrame中選擇任何一個研究對象,以下函數將為此研究創建“數據”(或跟蹤),并將其返回。 就這么簡單。 (請注意,我使用math.log()函數來獲得縮放的大小,但是如果您的值非常接近或包含負數,則可能應該更改它。)

3.創建布局 (3. Creating the layout)

I put up a default layout with all the basic features, which looks good for most plots, but you can play around with the parameters (see docs) to suit your needs.

我設置了具有所有基本功能的默認布局,該布局對大多數圖而言都不錯,但是您可以根據需要使用參數(請參閱docs )。

Note : In case you prefer a simple dark/light theme , change the style parameter from “satellite-and-streets” to “dark” or “light” respectively . The accesstoken used here is talked about at the end
注意:如果您希望使用簡單的深色/淺色主題,請將樣式參數分別從“衛星和街道”更改為“深色”或“淺色”。 最后討論這里使用的訪問令牌

Now that our basic layout is created , we need to update a few things which are not quite the same for every plot. So with another function you can update the basic annotations (shown below) . But again, there are plenty other things you can add here like font details, buttons ,drop down menus etc.

現在,我們已經創建了基本布局,我們需要更新一些與每個圖都不完全相同的東西。 因此,使用另一個功能,您可以更新基本注釋(如下所示)。 但同樣,您可以在此處添加很多其他內容,例如字體詳細信息,按鈕,下拉菜單等。

4.創建圖 (4. Creating the Plot)

Finally, now that we have the traces for all the studies, and a specific layout for each of them as well, we can create the figure and plot it.

最后,現在我們有了所有研究的痕跡,并且每個研究都有特定的布局,我們可以創建圖形并將其繪制出來。

Just to make repeated usage easier, and not to have the need to call all these functions over and over again , putting all of them together under a single function makes more sense. This also makes your code more readable at the end of the day.

只是為了使重復使用變得更容易,而不是需要一遍又一遍地調用所有這些函數,將所有這些函數放在一個函數下才有意義。 這也使您的代碼在一天結束時更具可讀性。

A single function which calls all the previous functions . You are now always one function call away from creating your plot :)
一個調用所有先前函數的函數。 現在,您始終只需調用一個函數即可創建繪圖:)

這些功能的實現 (Implementation of these functions)

Function calls and examples

函數調用和示例

You’ve probably started wondering why you needed to make those accounts in Mapbox and Chart Studio at the beginning , and not use them till the very end. But in fact the last thing we have to do before we get our plot on a platter, is to enter our credentials for access to these mapping interfaces.

您可能已經開始想知道為什么您需要開始就在MapboxChart Studio中創建這些帳戶,而直到最后才使用它們。 但是實際上,在將圖放入盤中之前,我們要做的最后一件事是輸入憑據以訪問這些映射界面。

(They can be found here (mapbox_access_token) and here (chart-studio credentials))

(可以在此處(mapbox_access_token)和此處(圖表工作室憑據)找到它們 )

Now everything is done and dusted, like I promised, you are now just a few lines of code away from plotting your map.

正如我所承諾的那樣,現在一切都已完成并完成了工作,現在您僅需要幾行代碼即可繪制地圖。

The format of the code to plot the map is as follows :

繪制地圖的代碼格式如下:

colors and number of objects of study (and their names) are not fixed, this is just an example. The code takes care of any number of objects of study in the nested list.
顏色和學習對象的數量(及其名稱)不是固定的,這只是一個例子。 該代碼可處理嵌套列表中任意數量的學習對象。

畫廊 (GALLERY)

After running your code, you should be getting a plots similar to these:

運行代碼后,您應該得到與以下類似的圖:

Example plot 1 : made with data taken from John Hopkins COVID -19 repository
樣例 1:使用John Hopkins COVID -19信息庫中的數據制作
Example plot 2: made with data taken from Kaggle (Census report on population distribution in India as of 2001)
示例圖2:使用Kaggle的數據制作而成(截至2001年的印度人口分布普查報告)

結論 (Conclusion)

I’m a 17 year old, who was introduced to the Plotly library while I was working on a project for my final exam. The library was extremely vast, and had so many applications for different kind of DataFrames. So at first , it was slightly overwhelming and it had too much to choose from.

我今年17歲,在我為期末考試設計項目時被介紹給Plotly圖書館。 該庫非常龐大,并且針對不同種類的DataFrame有許多應用程序。 因此,起初,它有點讓人不知所措,并且有太多選擇。

The functions I created are as general and as accommodative as I could make it ,but there will be a few tweaks you will need to make, to get the best possible map (like zoom, hover_info, or centre coordinates…depending on the location you are plotting on). So I hope these functions help you regardless of the kind or complexity of the DataFrame you have, and make plotting maps an easier and less daunting task from now on.

我創建的功能既通用又靈活,但是您需要做一些調整,以獲得最佳的地圖(如zoom,hover_info或center坐標……取決于您的位置)正在密謀)。 因此,我希望這些功能對您有用,無論您使用的DataFrame的種類或復雜程度如何,并使繪圖地圖從現在開始都變得更輕松,更輕松。

A fully commented code with a few examples can be found in my GitHub Repository

可以在我的GitHub存儲庫中找到帶有一些示例的完整注釋代碼

翻譯自: https://medium.com/swlh/how-to-plot-a-satellite-view-of-a-map-for-any-dataframe-in-python-using-plotly-d6211b0e3ffa

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388028.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388028.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388028.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java入門系列-26-JDBC

認識 JDBC JDBC (Java DataBase Connectivity) 是 Java 數據庫連接技術的簡稱,用于連接常用數據庫。 Sun 公司提供了 JDBC API ,供程序員調用接口和類,集成在 java.sql 和 javax.sql 包中。 Sun 公司還提供了 DriverManager 類用來管理各種不…

3.19PMP試題每日一題

在房屋建造過程中,應該先完成衛生管道工程,才能進行電氣工程施工,這是一個:A、強制性依賴關系B、選擇性依賴關系C、外部依賴關系D、內部依賴關系 作者:Tracy19890201(同微信號)轉載于:https://…

Can't find temporary directory:internal error

今天我機子上的SVN突然沒有辦法進行代碼提交了,出現的錯誤提示信息為: Error:Cant find temporary directory:internal error 然后試了下其他的SVN源,發現均無法提交,并且update時也出現上面的錯誤信息。對比項目文件…

snowflake 數據庫_Snowflake數據分析教程

snowflake 數據庫目錄 (Table of Contents) Introduction 介紹 Creating a Snowflake Datasource 創建雪花數據源 Querying Your Datasource 查詢數據源 Analyzing Your Data and Adding Visualizations 分析數據并添加可視化 Using Drilldowns on Your Visualizations 在可視化…

jeesite緩存問題

jeesite,其框架主要為: 后端 核心框架:Spring Framework 4.0 安全框架:Apache Shiro 1.2 視圖框架:Spring MVC 4.0 服務端驗證:Hibernate Validator 5.1 布局框架:SiteMesh 2.4 工作流引擎…

高級Python:定義類時要應用的9種最佳做法

重點 (Top highlight)At its core, Python is an object-oriented programming (OOP) language. Being an OOP language, Python handles data and functionalities by supporting various features centered around objects. For instance, data structures are all objects, …

Java 注解 攔截器

場景描述:現在需要對部分Controller或者Controller里面的服務方法進行權限攔截。如果存在我們自定義的注解,通過自定義注解提取所需的權限值,然后對比session中的權限判斷當前用戶是否具有對該控制器或控制器方法的訪問權限。如果沒有相關權限…

醫療大數據處理流程_我們需要數據來大規模改善醫療流程

醫療大數據處理流程Note: the fictitious examples and diagrams are for illustrative purposes ONLY. They are mainly simplifications of real phenomena. Please consult with your physician if you have any questions.注意:虛擬示例和圖表僅用于說明目的。 …

What's the difference between markForCheck() and detectChanges()

https://stackoverflow.com/questions/41364386/whats-the-difference-between-markforcheck-and-detectchanges轉載于:https://www.cnblogs.com/chen8840/p/10573295.html

ASP.NET Core中使用GraphQL - 第七章 Mutation

ASP.NET Core中使用GraphQL - 目錄 ASP.NET Core中使用GraphQL - 第一章 Hello WorldASP.NET Core中使用GraphQL - 第二章 中間件ASP.NET Core中使用GraphQL - 第三章 依賴注入ASP.NET Core中使用GraphQL - 第四章 GrahpiQLASP.NET Core中使用GraphQL - 第五章 字段, 參數, 變量…

POM.xml紅叉解決方法

方法/步驟 1用Eclipse創建一個maven工程,網上有很多資料,這里不再啰嗦。 2右鍵maven工程,進行更新 3在彈出的對話框中勾選強制更新,如圖所示 4稍等片刻,pom.xml的紅叉消失了。。。

JS前臺頁面驗證文本框非空

效果圖&#xff1a; 代碼&#xff1a; 源代碼&#xff1a; <script type"text/javascript"> function check(){ var xm document.getElementById("xm").value; if(xm null || xm ){ alert("用戶名不能為空"); return false; } return …

python對象引用計數器_在Python中借助計數器對象對項目進行計數

python對象引用計數器前提 (The Premise) When we deal with data containers, such as tuples and lists, in Python we often need to count particular elements. One common way to do this is to use the count() function — you specify the element you want to count …

套接字設置為(非)阻塞模式

當socket 進行TCP 連接的時候&#xff08;也就是調用connect 時&#xff09;&#xff0c;一旦網絡不通&#xff0c;或者是ip 地址無效&#xff0c;就可能使整個線程阻塞。一般為30 秒&#xff08;我測的是20 秒&#xff09;。如果設置為非阻塞模式&#xff0c;能很好的解決這個…

經典問題之「分支預測」

問題 來源 &#xff1a;stackoverflow 為什么下面代碼排序后累加比不排序快&#xff1f; public static void main(String[] args) {// Generate dataint arraySize 32768;int data[] new int[arraySize];Random rnd new Random(0);for (int c 0; c < arraySize; c)data…

vi

vi filename :打開或新建文件&#xff0c;并將光標置于第一行首 vi n filename &#xff1a;打開文件&#xff0c;并將光標置于第n行首 vi filename &#xff1a;打開文件&#xff0c;并將光標置于最后一行首 vi /pattern filename&#xff1a;打開文件&#xff0c;并將光標置…

數字圖像處理 python_5使用Python處理數字的高級操作

數字圖像處理 pythonNumbers are everywhere in our daily life — there are phone numbers, dates of birth, ages, and other various identifiers (driver’s license and social security numbers, for example).電話號碼在我們的日常生活中無處不在-電話號碼&#xff0c;…

05精益敏捷項目管理——超越Scrum

00.我們不是不知道它會給我們帶來麻煩&#xff0c;只是沒想到麻煩會有這么多。——威爾.羅杰斯 01.知識點&#xff1a; a.Scrum是一個強大、特意設計的輕量級框架&#xff0c;器特性就是將軟件開發中在制品的數量限制在團隊層級&#xff0c;使團隊有能力與業務落班一起有效地開…

帶標題的圖片輪詢展示

為什么80%的碼農都做不了架構師&#xff1f;>>> <div> <table width"671" cellpadding"0" cellspacing"0"> <tr height"5"> <td style"back…

linux java 查找進程中的線程

這里對linux下、sun(oracle) JDK的線程資源占用問題的查找步驟做一個小結&#xff1b;linux環境下&#xff0c;當發現java進程占用CPU資源很高&#xff0c;且又要想更進一步查出哪一個java線程占用了CPU資源時&#xff0c;按照以下步驟進行查找&#xff1a;(一)&#xff1a;通過…