如何使用Plotly在Python中为任何DataFrame绘制地图的卫星视图

本文介绍了使用Chart-Studio和Mapbox作为Folium和Geemap的替代方案,来创建卫星视图地图。Chart Studio用于创建交互式图表,而Mapbox是位置数据平台,用于构建地图。在开始之前,需要在Mapbox和Plotly Chart Studio上创建账户。
摘要由CSDN通过智能技术生成

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应用程序中开始使用的数据集通常如下所示:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值