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.
Folium和Geemap可以說是繪制衛星視圖地圖或其他任何種類的最佳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-Studio和Mapbox是一種簡單的替代方法,公平地講,它們也會產生驚人的結果。 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應用程序中開始使用的數據集通常如下所示:
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 :
因此,我們將其轉換為如下形式:
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:
您只需要以下幾欄:
- A location column- containing the country name/district/state, etc. which will be used for naming the subtraces, 位置列-包含國家名稱/地區/州等,將用于命名子跡線,
- A list of columns whose data you would like to plot on your map(one or more than one), 您想在地圖上繪制其數據的列的列表(一個或多個),
- A column containing the latitude of these locations, 一列包含這些位置的緯度,
- 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。 讓我們繼續。
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 )。
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.
只是為了使重復使用變得更容易,而不是需要一遍又一遍地調用所有這些函數,將所有這些函數放在一個函數下才有意義。 這也使您的代碼在一天結束時更具可讀性。
這些功能的實現 (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.
您可能已經開始想知道為什么您需要一開始就在Mapbox和Chart 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 :
繪制地圖的代碼格式如下:
畫廊 (GALLERY)
After running your code, you should be getting a plots similar to these:
運行代碼后,您應該得到與以下類似的圖:
結論 (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,一經查實,立即刪除!