python画交互式地图

Welcome to The Beginner’s Guide to Building Interactive Maps in Python

欢迎使用Python构建交互式地图的初学者指南

In this post, I would like to show you how to create interactive climate maps using the Historical Climate Data, where you can visualize, examine, and explore the data. Data visualization plays an important role in representing data. Creating visualizations helps to present your analysis in an easier form of understanding. Especially when working with large datasets it is very easy to get lost, that’s when we can see the power of data visualization. In this exercise, we will work with climate data from Kaggle. We will build two interactive climate maps. The first one will be showing the climate change of each country, and the second one will be showing the temperature change over time. Let’s get started, we have a lot to do!

在本文中,我想向您展示如何使用历史气候数据创建交互式气候图,您可以在其中可视化,检查和探索数据。 数据可视化在表示数据中起着重要作用。 创建可视化有助于以一种更容易理解的方式呈现您的分析。 特别是在处理大型数据集时,很容易迷失方向,这就是我们可以看到数据可视化的强大功能。 在本练习中,我们将使用来自Kaggle的气候数据。 我们将构建两个交互式气候图。 第一个显示每个国家的气候变化,第二个显示随着时间的温度变化。 让我们开始吧,我们还有很多事要做!

Kaggle is the world’s largest data science community with powerful tools and resources to help you achieve your data science goals.

Kaggle是全球最大的数据科学社区,其功能强大的工具和资源可帮助您实现数据科学目标。

目录: (Table of Contents:)

  • Plotly密谋
  • Understanding the Data了解数据
  • Data Cleaning数据清理
  • Data Preprocessing数据预处理
  • Data Visualization数据可视化

密谋 (Plotly)

Plotly is Python graphing library that makes interactive, publication-quality graphs. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, polar charts, and bubble charts. It is also an open-source library.

Plotly是Python图形库,可制作交互式的,具有出版质量的图形。 有关如何制作折线图,散点图,面积图,条形图,误差线,箱形图,直方图,热图,子图,多轴图,极坐标图和气泡图的示例。 它也是一个开源库。

To learn more about Plotly: Plotly Graphing Library

要了解有关Plotly的更多信息: Plotly Graphing Library

了解数据 (Understanding the Data)

The Berkeley Earth Surface Temperature Study combines 1.6 billion temperature reports from 16 pre-existing archives. It is nicely packaged and allows for slicing into interesting subsets (for example by country). They publish the source data and the code for the transformations they applied.

伯克利地球表面温度研究结合了16个现有档案中的16亿个温度报告。 它包装精美,可以切成有趣的子集(例如,按国家/地区)。 他们为应用的转换发布源数据和代码。

Dataset can be found at the following link: Climate Data

可以在以下链接中找到数据集: 气候数据

The data folder includes the following datasets:

数据文件夹包含以下数据集:

  • Global Average Land Temperature by Country全球平均陆地温度(按国家)
  • Global Average Land Temperature by State全球各州平均陆地温度
  • Global Land Temperatures By Major City主要城市的全球陆地温度
  • Global Land Temperatures By City全球城市气温
  • Global Land and Ocean-and-Land Temperatures全球陆地和海洋和陆地温度

We will be working with the “Global Average Land Temperature by Country” dataset, this data fits better for our goal because we are going to build interactive climate maps, and having a data filtered by country will make our life much easier.

我们将使用“按国家/地区划分的全球平均陆地温度”数据集,此数据更适合我们的目标,因为我们将构建交互式气候图,并且按国家/地区过滤数据将使我们的生活变得更加轻松。

图书馆 (Libraries)

We will need three main libraries to get started. When we come to visualization I will ask you to import a couple more sub-libraries, which are also known as library components. For now, we are going to import the following libraries:

我们将需要三个主要的库来开始。 进行可视化时,我将要求您导入几个子库,这些子库也称为库组件。 现在,我们将导入以下库:

import numpy as npimport pandas as pdimport plotly as py

If you don’t have these libraries, don’t worry. It is super easy to install them, as you can see below:

如果您没有这些库,请不要担心。 安装它们非常容易,如下所示:

pip install numpy pandas plotly

读取数据 (Read Data)

df = pd.read_csv("data/GlobalLandTemperaturesByCountry.csv")print(df.head())
head

print(df.tail())
tail
尾巴
# Checking the null values in each columndf.isnull().sum()
nulls
空值

数据清理 (Data Cleaning)

Data Science is more about understanding the data, and data cleaning is a very important part of this process. What makes the data more valuable depends on how much we can get from it. Preparing the data well will make your data analysis results more accurate.

数据科学更多地是关于理解数据的,数据清理是此过程中非常重要的一部分。 什么使数据更有价值取决于我们可以从中获得多少。 做好数据准备将使您的数据分析结果更加准确。

Let’s start with cleaning process. Firstly, let’s start by dropping the “AverageTemperatureUncertainty” column, because we don’t need it.

让我们从清洁过程开始。 首先,让我们开始删除“ AverageTemperatureUncertainty ”列,因为我们不需要它。

df = df.drop("AverageTemperatureUncertainty", axis=1)

Then, let’s rename the column names to have a better look. As you can see above, we are using a method called rename. Isn’t that cool how easy to rename a column name.

然后,让我们重命名列名称以使其外观更好。 如您在上面所看到的,我们正在使用一种称为重命名的方法。 重命名列名称的难易程度不是很酷。

df = df.rename(columns={'dt':'Date'})df = df.rename(columns={'AverageTemperature':'AvTemp'})

Lastly for the data cleaning, let’s drop the rows with the null values so that they don’t effect our analysis. As we checked earlier, we have around 32000 rows with null values in AverageTemperature column. And in total we have around 577000 rows, so dropping them is not a big deal. But in some cases, there are a couple other methods to handle null values.

最后,为了进行数据清理,让我们删除具有空值的行,以免影响我们的分析。 正如我们之前所检查的,AverageTemperature列中大约有32000行具有空值。 总共有大约577000行,因此删除它们并不是什么大问题。 但是在某些情况下,还有其他几种方法可以处理空值。

df = df.dropna()

Now, let’s have a look at our dataframe. I will print the first 10 rows using the head method.

现在,让我们看一下我们的数据框。 我将使用head方法打印前10行。

df.head(10)
result
结果

数据预处理 (Data Preprocessing)

This step is also known as data manipulation, where we filter the data so that we can focus on a specific analysis. Especially when working with big datasets, data preprocessing/ filtering is a must. For example, our historical climate data is showing temperatures for all 12 months between 1744 to 2013, so it’s actually a very wide range. Using data filtering techniques, we will focus on a smaller range like between 2000 to 2002.

此步骤也称为数据处理,其中我们对数据进行过滤,以便我们可以专注于特定的分析。 特别是在处理大型数据集时,必须进行数据预处理/过滤。 例如,我们的历史气候数据显示了1744年至2013年之间的所有12个月的温度,因此实际上范围很广。 使用数据过滤技术,我们将专注于较小的范围,例如2000到2002年之间。

比较运算符 (Comparison Operators)

  • <<
  • >>
  • <=<=
  • >=> =
  • ====
  • !=!=

We will use these operators to compare a specific value to values in the column. The result will be a series of booleans: True and Falses. True if the comparison is right, false if the comparison is not right.

我们将使用这些运算符将特定值与列中的值进行比较。 结果将是一系列布尔值:True和Falses。 如果比较正确,则为true;如果比较不正确,则为false。

分组依据 (Grouping by)

In this step, we are grouping the dataframe by Country name and the date columns. And also, sorting the values by date from latest to earliest time.

在此步骤中,我们将按国家/地区名称和日期列对数据框进行分组。 而且,还可以按日期从最晚到最早的时间对值进行排序。

df_countries = df.groupby( ['Country','Date']).sum().reset_index().sort_values('Date', ascending=False)
result
结果

屏蔽数据范围 (Masking by the data range)

start_date = '2000-01-01' end_date = '2002-01-01' mask = (df_countries['Date'] > start_date) & (df_countries['Date'] <= end_date) df_countries = df_countries.loc[mask] df_countries.head(10)
result
结果

As you can see above, the dataframe is looking great. Sorted by date and filtered by country name. We can find the average temperature in each month of each country by looking at this dataframe. Here comes the fun part, which is data visualization. Are you ready?

正如您在上面看到的,数据框看起来很棒。 按日期排序并按国家/地区名称过滤。 通过查看此数据框,我们可以找到每个国家/地区每个月的平均温度。 这是有趣的部分,它是数据可视化。 你准备好了吗?

数据可视化 (Data Visualization)

情节的组成 (Components of Plotly)

Before we start, as mentioned earlier there are couple sub-libraries to import to enjoy data visualization. These sub-libraries are also known as Components.

在开始之前,如前所述,有几个子库需要导入才能享受数据可视化。 这些子库也称为组件。

#Plotly Componentsimport plotly.express as pximport plotly.graph_objs as gofrom plotly.subplots import make_subplotsfrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

气候变化图 (Climate Change Map)

Perfect, now by running the following code you will see the magic happening.

完美,现在通过运行以下代码,您将看到魔术的发生。

#Creating the visualizationfig = go.Figure(data=go.Choropleth( locations = df_countries['Country'], locationmode = 'country names', z = df_countries['AvTemp'], colorscale = 'Reds', marker_line_color = 'black', marker_line_width = 0.5, ))fig.update_layout( title_text = 'Climate Change', title_x = 0.5, geo=dict( showframe = False, showcoastlines = False, projection_type = 'equirectangular' ) ) fig.show()
climate change interactive map
气候变化互动地图

气候变化的时间表 (Climate Change by Timeline)

# Manipulating the original dataframe df_countrydate = df_countries.groupby(['Date','Country']). sum().reset_index() #Creating the visualization fig = px.choropleth(df_countrydate, locations="Country", locationmode = "country names", color="AvTemp", hover_name="Country", animation_frame="Date" ) fig.update_layout( title_text = 'Average Temperature Change', title_x = 0.5, geo=dict( showframe = False, showcoastlines = False, )) fig.show()

结果 (Results)

Both are the same map, in the first one you can see the change in average temperature. And in the second graph, I am just hovering over some countries, which is showing more detailed information about each of them.

两者是同一张图,在第一个图中,您可以看到平均温度的变化。 在第二张图中,我只是将鼠标悬停在某些国家/地区上,该国家/地区显示了有关每个国家/地区的更详细的信息。

interactive map 1
互动地图1
interactive map 2
互动地图2

Thank you for reading this post, I hope you enjoyed and learn something new today. Feel free to contact me through my blog if you have any questions while implementing the code. I will be more than happy to help. You can find more posts I’ve published related to Python and Machine Learning. Stay safe and happy coding!

感谢您阅读这篇文章,希望您今天喜欢并学到一些新东西。 如果在实施代码时有任何疑问,请随时通过我的博客与我联系 。 我将非常乐意提供帮助。 您可以找到我发布的更多有关Python和机器学习的文章。 保持安全快乐的编码!

I am Behic Guven, and I love sharing stories on creativity, programming, motivation, and life.

我是Behic Guven,我喜欢分享有关创造力,编程,动力和生活的故事。

Follow my blog and Towards Data Science to stay inspired.

关注 我的博客 迈向数据科学 ,保持灵感。

相关文章 (Related Posts)

翻译自: https://towardsdatascience.com/building-interactive-maps-in-python-the-beginners-guide-5711dd66257e

python画交互式地图


http://www.taodudu.cc/news/show-863868.html

相关文章:

  • 大疆 机器学习 实习生_我们的数据科学机器人实习生
  • ai人工智能的本质和未来_人工智能的未来在于模型压缩
  • tableau使用_使用Tableau探索墨尔本房地产市场
  • 谷歌云请更正这张卡片的信息_如何识别和更正Google Analytics(分析)报告中的(未设置)值
  • 科技情报研究所工资_我们所说的情报是什么?
  • 手语识别_使用深度学习进行手语识别
  • 数据科学的5种基本的面向业务的批判性思维技能
  • 大数据技术 学习之旅_数据-数据科学之旅的起点
  • 编写分段函数子函数_编写自己的函数
  • 打破学习的玻璃墙_打破Google背后的创新深度学习
  • 向量 矩阵 张量_张量,矩阵和向量有什么区别?
  • monk js_使用Monk AI进行手语分类
  • 辍学的名人_辍学效果如此出色的5个观点
  • 强化学习-动态规划_强化学习-第5部分
  • 查看-增强会话_会话式人工智能-关键技术和挑战-第2部分
  • 我从未看过荒原写作背景_您从未听说过的最佳数据科学认证
  • nlp算法文本向量化_NLP中的标记化算法概述
  • 数据科学与大数据排名思考题_排名前5位的数据科学课程
  • 《成为一名机器学习工程师》_如何在2020年成为机器学习工程师
  • 打开应用蜂窝移动数据就关闭_基于移动应用行为数据的客户流失预测
  • 端到端机器学习_端到端机器学习项目:评论分类
  • python 数据科学书籍_您必须在2020年阅读的数据科学书籍
  • ai人工智能收入_人工智能促进收入增长:使用ML推动更有价值的定价
  • 泰坦尼克数据集预测分析_探索性数据分析—以泰坦尼克号数据集为例(第1部分)
  • ml回归_ML中的分类和回归是什么?
  • 逻辑回归是分类还是回归_分类和回归:它们是否相同?
  • mongdb 群集_通过对比群集分配进行视觉特征的无监督学习
  • ansys电力变压器模型_变压器模型……一切是如何开始的?
  • 浓缩摘要_浓缩咖啡的收益递减
  • 机器学习中的无监督学习_无监督机器学习中聚类背后的直觉

python画交互式地图_使用Python构建交互式地图-入门指南相关推荐

  1. python画地球代码_用Python制作中国地图、地球平面图及球形图

    绘制地图在python中主要用到的 basemap 库,这个库是 matplotlib 库中一个用于在 Python 中绘制地图上的 2D 数据的工具包. 安装库: 1.安装 geos 库:Pytho ...

  2. 用python画动态樱花_利用python画一棵漂亮的樱花树,turtle画图代码大全,此处感谢知乎大佬小白...

    利用python画一棵漂亮的樱花树,turtle画图代码大全,此处感谢知乎大佬小白 此处感谢知乎大佬 小白练手 练习一下比较流行的turtle(海龟库) 画一棵漂亮的樱花树,效果如下: ps: 是动态 ...

  3. python画猪头_使用Python画小猪佩奇 社会人标配

    看了一些用python实现小猪佩奇画画的帖子,向自己实现下,以此记录. 社会人的标配是谁,当然是吹风机小猪佩奇身上纹. 我自己尝试画过小猪配齐但是感觉眼睛特别难画,画出来的猪头没有立体感,眼睛画不好整 ...

  4. python画雪花流程图_用python画雪花,Python

    # 用Python画雪花 # 学习https://www.toutiao.com/i6787554710594257420/?tt_from=weixin&utm_campaign=clien ...

  5. 如何用python画金字塔形状_用Python画小猪佩奇,落叶树和动漫人物

    本文目录:小猪佩奇-落叶树-魔法少女 本文作者:小赖同学 ​ 最近在浏览Python文章时,无意中发现了一个好玩的画图工具turtle,看到各路大神竟然用Python画了一个图片出来,无法用言语来形容 ...

  6. 如何用python画金字塔形状_用 Python 画一只福鼠

    今年这个鼠年,尽管只是刚过了一个开头,但现实仿佛把我们很多年才需要经历的不好的事情,都在一瞬间抛向了我们,这个鼠年好像缺了点福气,本节我们使用 Python 画一只福鼠,为大家增添点福气,我们需要用到 ...

  7. 如何用python画爱心代码_用 python 画爱心代码讲解

    学计算机的男生发这个给我看是什么意思?​www.zhihu.com 原理其实挺简单的. 代码网上也有. 最难的部分前人都告诉我们了, 心形可画. 要自己推导通过泰勒各种扭也可以. 通过肉眼扭我感觉也不 ...

  8. python画卡通人物_用Python 绘制儿童卡通人物,一起过儿童节

    又到一年一度的国际儿童节,今天我们来学习一下用 Python 的 Turtle 库绘制童年的卡通人物,一起做回年轻的那个少年. Turtle图形库,又称海龟库,是 Python 语言中一个很流行的绘制 ...

  9. python画圆形螺旋线_这个Python项目,一秒生成可爱像素风图片

    本文转自:量子位,作者郭一璞整理来自:大邓和他的 Python像文字云一样,用各种小图拼出大的图片,构建一个像素风的世界,就像<我的世界>里一样,一定非常有趣.还可以拿来做拼贴画.十字绣等 ...

  10. python画圆形螺旋线_用Python一秒生成复古像素图片

    本文转自:量子位,作者郭一璞整理来自:大邓和他的 Python像文字云一样,用各种小图拼出大的图片,构建一个像素风的世界,就像<我的世界>里一样,一定非常有趣.还可以拿来做拼贴画.十字绣等 ...

最新文章

  1. 一文告诉你,为什么要研究JVM原理
  2. The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
  3. 巧用 PHP 数组函数
  4. 语言代码编程大赛简讯_精品干货:C语言的高效编程与代码优化
  5. activemq 安全连接
  6. Entity Framework Core 批处理语句
  7. 软件测试推荐专业,软件测试专业老师推荐信
  8. 【渝粤教育】国家开放大学2018年春季 0689-22T老年心理健康 参考试题
  9. leetcode841. 钥匙和房间(bfs)
  10. mysql 5.6.13-winx64_MySQL-5.6.13 zip解压版的安装与配置教程
  11. AVR工具指南(二)
  12. linux磁盘阵列扩容,linux raid1扩容的方法
  13. 使用虚幻4开发HoloLens的准备工作
  14. android输入法剪贴板,手机写作利器:输入法剪贴板
  15. SYSAUX表空间占用过大情况下的处理(AWR信息过多)
  16. Tensorflow2 model.compile()理解
  17. 理解softmax函数
  18. 阿里图标库彩色图标使用
  19. Combo Box 组合框
  20. 机器学习基础(六)贝叶斯统计

热门文章

  1. 《防患未然:实施情报先导的信息安全方法与实践》——2.8 小结
  2. TabLayout 与 FragmentTabHost
  3. 3DMax的OFusion插件的使用问题
  4. 安装中文版cacti监控华为交换机流量并实现95计费
  5. SQL中的存储过程中的事务处理。备忘
  6. in作为介词的用法_英语中in/on/at/to/from/by/with/for/about/after/before的用法
  7. python调用c的配置文件_python调用c
  8. centos 7.6安装java_安装 QRadar Community Edition
  9. qt c++ 图片预览_这是Google Pixel 4上的新动态壁纸的预览
  10. CIKM 2021 | 图模型在广告检索(Ad Retrieval)中的应用