perl 传单引号_使用geopandas和传单在python和r中绘制精美的地理图

本文主要是介绍perl 传单引号_使用geopandas和传单在python和r中绘制精美的地理图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

perl 传单引号

There are several geographic libraries that are available for plotting location information on a map. I’ve previously written about the same topic here but since then I’ve used these libraries more extensively as well as got introduced to new ones. After using and examining several libraries, I found that GeoPandas and the Leaflet libraries are two of the most easy to use and highly customizable libraries. The code is available as a GitHub repo:

有几个地理库可用于在地图上绘制位置信息。 我以前在这里曾写过同一主题,但是从那时起,我就更广泛地使用了这些库,并且将它们引入了新库。 使用并检查了几个库之后,我发现GeoPandas和Leaflet库是最易于使用和高度可定制的两个库。 该代码可作为GitHub存储库提供:

数据集 (Dataset)

The dataset is taken from Kaggle. It includes the population density (population per square Km) for various countries across the world. The dataset includes the values ranging from 1961 to 2015.

该数据集取自Kaggle 。 它包括世界各国的人口密度(每平方千米人口)。 数据集包括从1961年到2015年的值。

Image for post
Dataset
数据集

The figure above shows that we need to skip the first 4 rows when reading in the dataset, so we directly start with the data. Columns such as “1960” are empty and hence they can be removed. Also, data for some countries like Belgium is missing so we’ll remove these records from our collection.

上图显示,在读取数据集时,我们需要跳过前4行,因此我们直接从数据开始。 诸如“ 1960”的列为空,因此可以将其删除。 另外,某些国家(例如比利时)的数据丢失了,因此我们将从集合中删除这些记录。

使用GeoPandas的静态图(在Python中) (Static plots using GeoPandas (in Python))

导入库 (Import libraries)

We’ll import the library pandas to read the dataset and then plot the maps using geopandas. Note that to remove unnecessary warnings, I added the specific command.

我们将导入库pandas以读取数据集,然后使用geopandas绘制地图。 请注意,为消除不必要的警告,我添加了特定命令。

import pandas as pd
import geopandas as gpdimport warnings
warnings.filterwarnings('ignore')%matplotlib inline

导入数据集(Import dataset)

Next, we’ll import the dataset. As mentioned before, I skip the first 4 rows.

接下来,我们将导入数据集。 如前所述,我跳过了前4行。

dataset = pd.read_csv("data/dataset.csv", skiprows=4)
dataset = dataset.drop(["Indicator Name", "Indicator Code", "1960", "2016", "Unnamed: 61"], axis = 1)
dataset = dataset.dropna(how = 'any')
dataset["avgPopulationDensity"] = dataset.iloc[:, 2:].mean(axis = 1)
dataset.head()

I remove all the extra columns that I don’t need (“Indicator Name”, “Indicator Code”, “1960”, “2016”, “Unnamed: 61”) using the drop() method. I remove all records with any NULL values using dropna()and finally calculate the average population density across years 1961 to 2015 into a new column avgPopulationDensity.

我使用drop()方法删除了所有不需要的额外列(“指标名称”,“指标代码”,“ 1960”,“ 2016”,“未命名:61”)。 我使用dropna()删除所有具有任何NULL值的记录,最后将1961年至2015年之间的平均人口密度计算到新列avgPopulationDensity

Image for post
Resulting dataset
结果数据集

绘图数据(Plot data)

We first load in the world baseplot directly from geopandas and then merge the dataset we refined above with this baseplot data. Then, we simply plot th data world shaded by the values in the avgPopulationDensity column. We shade the countries in the Reds colormap which means countries with higher average population densities are darker than other countries.

我们首先直接从geopandas加载世界基准图,然后将我们在上面精炼的数据集与此基准图数据合并。 然后,我们简单地绘制由avgPopulationDensity列中的值阴影的数据world 。 我们在“ Reds配色图中为国家/地区加阴影,这意味着平均人口密度较高的国家/地区比其他国家更暗。

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world.merge(dataset, how = 'left', left_on = 'iso_a3', right_on = 'Country Code')
world.plot(column = 'avgPopulationDensity', edgecolor = 'black',figsize = (16, 16),cmap = 'Reds')
Image for post
Average Population Density plot (using GeoPandas)
平均人口密度图(使用GeoPandas)

We can clearly see that countries in Asia have more population density values especially India, Bangladesh, Korea and Japan.

我们可以清楚地看到,亚洲国家的人口密度值更高,尤其是印度,孟加拉国,韩国和日本。

使用Leaflet的交互式图(在R中) (Interactive plots using Leaflet (in R))

导入库 (Import libraries)

We’ll load in the leaflet library for generating plots and the sf library for reading in shapefiles.

我们将加载用于生成图形的leaflet库,并加载用于读取shapefile的sf库。

library(leaflet)
library(sf)

导入数据集(Import dataset)

Just like in Python, we’d import in the dataset skipping first 4 rows, remove extra columns, remove rows with missing values and create the avgPopulationDensity column.

就像在Python中一样,我们将跳过前4行导入数据集中,删除多余的列,删除缺少值的行并创建avgPopulationDensity列。

dataset <- read.csv("data/dataset.csv", skip = 4)
dataset[, c("Indicator.Name", "Indicator.Code", "X1960", "X2016", "X")] <- NULL
dataset <- dataset[complete.cases(dataset), ]
dataset["avgPopulationDensity"] <- rowSums(dataset[,3:57])

It’s important that we also load in the shapefile needed for the plotting. Thus, we use the st_read method and merge the dataset with this shapefile into a variable named shapes.

重要的是,我们还要加载绘图所需的shapefile。 因此,我们使用st_read方法并将具有此shapefile的数据集合并到一个名为shapes的变量中。

shapes <- st_read("data/countries_shp/countries.shp")
shapes <- merge(shapes, dataset, by.x = 'ISO3', by.y = 'Country.Code')

绘图数据(Plot data)

Before we plot the data, we first define the color palette we want to use. I defined the shades of red for various bins and based on that I will shade the resultant plot. As this is a custom palette, it’s colors will be different from the one we see in the GeoPandas plot.

在绘制数据之前,我们首先定义要使用的调色板。 我为各种垃圾箱定义了红色阴影,并以此为基础绘制了结果图。 由于这是一个自定义调色板,因此其颜色将与我们在GeoPandas图中看到的颜色不同。

colors <- c("#8B0000","#FF0000", "#FFA07A")
bins <- c(100000, 20000, 5000, 0)color_pal <- leaflet::colorBin(colors, domain = shapes$avgPopulationDensity, bins = bins)

Next, we plot the leaflet geoplot. The leaflet() starts creating the plot and addTiles() adds the tiles for the countries. Next, we set the view of the map by defining the latitude and longitude values as well as the zoom so we can see the whole map at once. We can change the values if needed but as it is an interactive map, we can do so dynamically too.

接下来,我们绘制传单地理图。 leaflet()开始创建图, addTiles()添加国家/地区的图块。 接下来,我们通过定义纬度和经度值以及缩放比例来设置地图视图,以便我们可以一次查看整个地图。 我们可以根据需要更改值,但由于它是一个交互式地图,因此也可以动态更改。

leaflet(shapes) %>% addTiles() %>%setView(lat = 10, lng = 0, zoom = 1) %>%addPolygons(fillColor = ~color_pal(shapes$avgPopulationDensity),fillOpacity = 0.7,label = shapes$Country.Name,stroke = TRUE,color = 'black',weight = 1,opacity = 1)

Finally, based on the avgPopulationDensity column, we color grade the whole plot and attach country names as labels. We customize the look of each country by adding in strokes (STROKE = TRUE), and then setting them to be black with weight = 1, defining the width of each boundary.

最后,基于avgPopulationDensity列,我们对整个绘图进行颜色分级,并附加国家名称作为标签。 我们通过添加笔画( STROKE = TRUE ),然后将它们设置为weight = 1 black ,定义每个边界的宽度,来自定义每个国家/地区的外观。

Image for post
Average Population Density plot (using LeafLet)
平均人口密度图(使用LeafLet)

We observe that countries in Asia like India, Bangladesh, China, Japan and others have high population densities than other countries.

我们观察到,印度,孟加拉国,中国,日本等亚洲国家的人口密度比其他国家高。

结论 (Conclusion)

From the two examples above, we see how easy it is to create beautiful static and dynamic geographic plots. Plus, there are tons of customizations that we can add.

从上面的两个示例中,我们看到了创建漂亮的静态和动态地理图非常容易。 另外,我们可以添加大量自定义设置。

If you have any suggestions, questions or ideas, please let me know down below.

如果您有任何建议,问题或想法,请在下面告诉我。

翻译自: https://towardsdatascience.com/elegant-geographic-plots-in-python-and-r-using-geopandas-and-leaflet-27126b60bace

perl 传单引号


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

相关文章:

  • java-IO流中的对象流和序列化理解
  • Node.js(尚硅谷最经典Node.js快速入门学习笔记)
  • 单片机缩写
  • 语音低速率编码技术和数字水印技术的研究
  • 无线网络和技术概念
  • 【专业造轮子】:一位大神的编程之路,让我大吃一惊!
  • Lombok注解@SneakyThrows的作用
  • 电池模拟器之电阻分压1
  • 杰里之693N 纽扣电池供电(硬件有特殊接法--自行对原理图【篇】
  • 电池BMS电流校准原理
  • 电池级碳酸锂、碳酸氢锂除钙镁的工作原理
  • 动力电池原理说明及应用概述
  • 电池工作原理:一种通俗易懂的讲解
  • 【操作系统】总结 (一) 计组部分知识
  • 云网融合,SDN在云数据中心的应用
  • 可口的故事
  • linux下安装qt、qt触摸屏校准tslib
  • 锐捷网络:缔造创新型无线WiFi
  • 系统设计时如何应对现今主流的攻击?
  • 细节选择成功——谈中小企业网站建设之网络营
  • 案例分享:广深沿江高速已部署93根多功能智能杆
  • Gartner:2021-2022年八大网络安全预测
  • 主机加固:系统加固的新概念
  • 网络安全如何解决攻防不对等和系统碎片化危机?
  • 利用网络安全路线图实现IT合规
  • 卷积神经网络与视觉计算,卷积神经网络的可视化
  • 人工神经网络与神经网络,完整的人工神经网络
  • Springboot 异步任务@Async 使用和失效分析
  • ESD与EOS失效案例分享
  • MYSQL索引使用案例分析
  • 这篇关于perl 传单引号_使用geopandas和传单在python和r中绘制精美的地理图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/202118

    相关文章

    Python办公自动化实战之打造智能邮件发送工具

    《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

    Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

    《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

    java使用protobuf-maven-plugin的插件编译proto文件详解

    《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

    Python包管理工具pip的升级指南

    《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

    SpringBoot线程池配置使用示例详解

    《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

    C++ Log4cpp跨平台日志库的使用小结

    《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

    Ubuntu如何分配​​未使用的空间

    《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

    Qt使用QSqlDatabase连接MySQL实现增删改查功能

    《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

    基于Python实现一个图片拆分工具

    《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

    Python中反转字符串的常见方法小结

    《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函