python bokeh_提升视觉效果:使用Python和Bokeh制作交互式地图

python bokeh

Let’s face it, fellow data scientists: our clients LOVE dashboards. Why wouldn’t they? Visualizing our data helps us tell a story. Visualization turns thousands of rows of data into a compelling and beautiful narrative. In fact, dashboard visualizations have become so commonplace that virtually every resume features at least one (maybe several) dashboard projects. How can you make yours stand out? Bar charts and time-series line graphs aren’t going to cut it.

面对现实吧,数据科学家们:我们的客户喜欢仪表板。 他们为什么不呢? 可视化我们的数据有助于我们讲故事。 可视化将成千上万行数据变成引人入胜且精美的叙述。 实际上,仪表板可视化已变得司空见惯,几乎每个简历都至少具有一个(可能是几个)仪表板项目。 如何使自己脱颖而出? 条形图和时间序列折线图不会削减它。

Enter: geographic data.What is geographic data? Broadly speaking, it’s anything you can put on a map. Does it have lat/lon coordinates? Does it exist in geographic space? That’s geographic data.

输入:地理数据。什么是地理数据? 广义上讲,您可以在地图上放置任何内容。 它有纬度/经度坐标吗? 它存在于地理空间中吗? 那是地理数据。

Maps help us tell stories. Think to some of your favorite novels — how many of them have maps in their pages? Maps immerse us in the environment of the story; in this case, the story of your client’s data.

地图可以帮助我们讲故事。 想一想您最喜欢的一些小说-他们中有几本在页面上有地图? 地图使我们沉浸在故事的环境中; 在这种情况下,就是客户数据的故事。

This article will teach you to deal with three types of maps: heatmaps, bubble maps, and categorical maps. By the end of this tutorial, you’ll be a spatial data wizard ready to set your dashboard visualizations apart from the rest. Let’s dive in!

本文将教您处理三种类型的地图:热图,气泡图和分类图。 在本教程结束时,您将成为一个空间数据向导,准备将您的仪表板可视化设置为与众不同。 让我们潜入吧!

笔记本 (Notebook)

For the complete code associated with this project and to see the interactive legend and hover tool-tips in action, see the Jupyter Notebook associated with this project!

有关与该项目关联的完整代码,以及正在使用的互动式图例和悬停工具提示,请参阅与此项目关联的Jupyter Notebook !

我们的数据 (Our Data)

For the purpose of this tutorial, we’ll be using data from the Armed Conflict Location Event Database (ACLED). I urge you to try and use your own data on this project! If that sounds too complex for you, you can get the data I used from the github repo associated with this article.

就本教程而言,我们将使用武装冲突位置事件数据库(ACLED)中的数据 。 我敦促您尝试在此项目上使用自己的数据! 如果这听起来对您来说太复杂了,那么您可以从与本文相关联的github存储库中获取我使用的数据。

简要免责声明: (A Brief Disclaimer:)

This article won’t go over exploratory analysis of the dataset; I’ll be operating under the assumption that you are already familiar with basic visualization techniques and terminology as well as basic concepts associated with the Bokeh, Numpy, and Pandas libraries. To view my exploratory analysis of the data, see this notebook.

本文将不对数据集进行探索性分析。 我将假设您已经熟悉基本的可视化技术和术语以及与Bokeh,Numpy和Pandas库相关的基本概念。 要查看我对数据的探索性分析,请参阅此笔记本 。

If this is your first time using Bokeh, try a few beginner tutorials from their user guide.

如果您是第一次使用Bokeh,请尝试阅读其用户指南中的一些初学者教程。

让我们潜入吧! (Let’s dive in!)

导入模块 (Import Modules)

We’ll be using all of Bokeh, Pandas, and Numpy in this project.

在此项目中,我们将使用所有Bokeh,Pandas和Numpy。

加载数据 (Load the Data)

You can access the data used for this project here. Import the data using pandas ‘read_csv()’ method. You’ll need the latitude and longitude columns to be float types so that we can convert them to web mercator units later on. Whatever column you choose to define the radius of your bubble map should be an int or float type. In this demonstration, it’s the fatalities column.

您可以在此处访问用于该项目的数据。 使用pandas'read_csv()'方法导入数据。 您需要将纬度经度列设置为浮点类型,以便稍后我们可以将其转换为网络墨卡托单位。 无论您选择定义气泡图半径的任何列,都应为intfloat类型。 在此演示中,它是“ 死亡人数”列。

初始化地图对象 (Initialize the Map Object)

Like many Python libraries, Bokeh is very object-oriented. Before we can plot our data, we must initialize a map object in the form of a Bokeh plotting figure. One important consideration when using Bokeh for map-making is that Bokeh uses mercator units for plotting. Most of your spatial data likely comes in the form of lat/long coordinates (commonly known as wgs84). First, we will convert these coordinates to mercator and then we will initialize the plot object for our data.

像许多Python库一样,Bokeh也是非常面向对象的。 在绘制数据之前,我们必须以Bokeh绘制图形的形式初始化地图对象。 使用散景进行地图绘制时,一个重要的考虑因素是散景使用墨卡托单位进行绘制。 您的大多数空间数据都可能以经/纬度坐标的形式出现(通常称为wgs84)。 首先,我们将这些坐标转换为墨卡托,然后初始化数据的plot对象。

To initialize the plotting object, we have to define the x and y range, or extent, for the map visibility. I want the extents of the map to have a constant proportion to the size of hexbins and bubbles that I will visualize later, so I establish the map scale using a variable. The number chosen for this is arbitrary — I simply played around with a few different numbers until I was happy with the zoom level. Setting it as one variable makes it very easy to change later!

要初始化绘图对象,我们必须定义地图可见性的x和y范围或范围。 我希望地图的范围与以后将可视化的六边形和气泡的大小具有恒定的比例,因此我使用变量来建立地图比例。 为此选择的数字是任意的-我只是简单地玩几个不同的数字,直到对缩放级别感到满意为止。 将其设置为一个变量可以很容易地在以后进行更改!

Additionally, you will have to select a tile provider for your map. I prefer Open Street Maps (OSM), but you can find a complete list of Bokeh’s supported map tiles here. Set the map level to ‘underlay’ and use the ‘output_notebook()’ method to display the plot in-line if you’re using a Jupyter notebook.

此外,您将不得不为地图选择一个瓷砖提供者。 我更喜欢开放式街道地图(OSM),但是您可以在此处找到Bokeh支持的地图图块的完整列表。 如果使用的是Jupyter笔记本,则将地图级别设置为“参考底图”,并使用“ output_notebook()”方法在线显示绘图。

创建地图生成功能 (Create Map Generation Functions)

First we will create a function for the hexbin map. Hexbins are mathematically more accurate than most standard heatmaps, especially for data that is presented elliptically or has a lower range of geographic precision. They also have the additional benefit of being visually appealing! You will also note that I created a hover tool that will display the count of items in each hex as well as the q,r coordinates associated with each hexagon.

首先,我们将为hexbin映射创建一个函数。 十六进制在数学上比大多数标准热图更准确,尤其是对于以椭圆形式显示或地理精度范围较低的数据。 它们还具有吸引人的视觉优势! 您还将注意到,我创建了一个悬停工具,该工具将显示每个十六进制中的项目计数以及与每个六边形关联的q,r坐标。

Next, we will write a function to create a bubble map. Bubble maps are useful for depicting scale of events. In this case, the radius of bubbles will be proportionate to the amount of fatalities caused by each event — this type of map is also great for depicting weather events, pandemic cases, and other scalar datasets.

接下来,我们将编写一个函数来创建气泡图。 气泡图对于描述事件的规模非常有用。 在这种情况下,气泡的半径将与每个事件导致的死亡人数成比例-这种类型的地图也非常适合描述天气事件,大流行病例和其他标量数据集。

让我们看看我们的工作! (Let’s See Our Work!)

Now simply input your parameters into the function you created! Note that in the functions I wrote, I set a few default parameters to make calling the function easier.

现在,只需将参数输入到您创建的函数中即可! 请注意,在我编写的函数中,我设置了一些默认参数以使调用函数更容易。

Once your plot appears in the window, try hovering over it with your mouse! You should see the map data change color as you hover over it in addition to informative tool-tips. Try clicking entries in the legend to toggle map layers on and off!

一旦情节出现在窗口中,请尝试将鼠标悬停在其上方! 将鼠标悬停在地图数据上时,除了提供提示性的工具提示外,您还应该看到地图颜色发生变化。 尝试单击图例中的条目以打开或关闭地图图层!

Image for post

为什么起作用? (Why Functions?)

We used functions to create our maps for ease of re-use. In the future, if we have data that we want to plot using a bubble map or heatmap, we can re-use these same functions rather than completely rewriting code. Using functions for common tasks is a good habit to get into, especially if you work on a team where team members may frequently use eachother’s code!

我们使用函数来创建地图以便于重复使用。 将来,如果我们有想要使用气泡图或热图绘制的数据,则可以重用这些相同的函数,而不必完全重写代码。 在常见任务中使用函数是一个好习惯,尤其是在团队中团队成员可能经常使用彼此代码的情况下!

分类数据的交互式图例 (Interactive Legends for Categorical Data)

You may have noticed in our above map that you can toggle layer visibility by clicking items in the legend. How can we make better use of this feature? Next, we will display our data categorically. For this data, I will be using event_type. Note that the code that follows is not designed for maximum efficiency or re-usability; but rather for readability. A beginner should be able to read and understand the next few steps. If you’re more advanced, try writing a function to make this plot more re-usable!

您可能已经在上面的地图中注意到,可以通过单击图例中的项目来切换图层可见性。 我们如何更好地利用此功能? 接下来,我们将分类显示数据。 对于此数据,我将使用event_type 。 请注意,以下代码并非旨在最大程度地提高效率或可重复使用性; 而是为了提高可读性。 初学者应该能够阅读和理解接下来的几个步骤。 如果您更高级,请尝试编写函数以使该图更可重用!

First, let’s initialize a new plot object.

首先,让我们初始化一个新的绘图对象。

There are many ways to accomplish this next step, but each factor of the categorical variable will need to be plotted separately to its own level. In this code, I initialized empty lists for each factor and will append the information associated with each type.

有许多方法可以完成此下一步,但是分类变量的每个因素都需要分别绘制到自己的水平。 在此代码中,我为每个因子初始化了空列表,并将附加与每种类型关联的信息。

For each empty list, an iterator searches the dataframe for events of that type and appends the list with information such as x and y coordinates, lat/lon coordinates, and other descriptive information. If you choose this method, you will have to do this for each empty list. You can also nest iterators together to accomplish this, but for demonstration purposes I chose to write each separate block for code readability — nested iterators are often difficult to understand without extensive use of comments.

对于每个空列表,迭代器都会在数据帧中搜索该类型的事件,并在列表中附加诸如x和y坐标,纬度/经度坐标以及其他描述性信息之类的信息。 如果选择此方法,则必须为每个空白列表执行此操作。 您也可以将迭代器嵌套在一起以实现此目的,但是出于演示目的,我选择编写每个单独的块来提高代码的可读性-如果不大量使用注释,嵌套的迭代器通常很难理解。

Once the lists are populated with the information associated with each category, each list gets plotted iteratively as a separate layer to the plotting object we created earlier. The reason we do this, as opposed to plotting all the data at once, is to enhance the toggle capability of the interactive legend. Now, in the resulting map plot, users can click different categories in the legend to filter the data by event type.

用与每个类别相关的信息填充列表后,每个列表将作为我们先前创建的绘图对象的单独图层进行迭代绘制。 与一次绘制所有数据相反,我们这样做的原因是要增强交互式图例的切换功能。 现在,在生成的地图绘图中,用户可以单击图例中的不同类别以按事件类型过滤数据。

Use the ‘show(row())’ method from Bokeh to display both maps simultaneously on a dashboard. Admire your work! Try hovering over map entries to get more information, and clicking the legend to filter the display. That’s it!

使用Bokeh的'show(row())'方法在仪表板上同时显示两个地图。 欣赏你的工作! 尝试将鼠标悬停在地图条目上以获取更多信息,然后单击图例以过滤显示。 而已!

Image for post

摘要 (Summary)

Now you can plot maps in Bokeh and create useful interactions that will allow your clients or customers to gain maximum insight from their spatial data. You learned how to create hexbin heatmaps, bubble maps, and interactive categorical maps. By combining these skills with other data visualization techniques, you will be ready to create visually stunning and useful dashboard visualizations for your next data science project!

现在,您可以在Bokeh中绘制地图并创建有用的交互作用,从而使您的客户可以从他们的空间数据中获得最大的洞察力。 您学习了如何创建六边形热图,气泡图和交互式分类图。 通过将这些技能与其他数据可视化技术相结合,您将为下一个数据科学项目创建视觉上令人惊叹且有用的仪表板可视化!

翻译自: https://towardsdatascience.com/level-up-your-visualizations-make-interactive-maps-with-python-and-bokeh-7a8c1da911fd

python bokeh

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388549.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用C#写 四舍五入函数(原理版)

doubled 0.06576523;inti (int)(d/0.01);//0.01决定了精度 doubledd (double)i/100;//还原 if(d-dd>0.005)dd0.01;//四舍五入 MessageBox.Show((dd*100).ToString()"%");//7%,dd*100就变成百分的前面那一部分了

浪里个浪 FZU - 2261

TonyY是一个喜欢到处浪的男人,他的梦想是带着兰兰姐姐浪遍天朝的各个角落,不过在此之前,他需要做好规划。 现在他的手上有一份天朝地图,上面有n个城市,m条交通路径,每条交通路径都是单行道。他已经预先规划…

C#设计模式(9)——装饰者模式(Decorator Pattern)

一、引言 在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手…

nosql_探索NoSQL系列

nosql数据科学 (Data Science) Knowledge on NoSQL databases seems to be an increasing requirement in data science applications, yet, the taxonomy is so diverse and problem-centered that it can be a challenge to grasp them. This post attempts to shed light on…

C++TCP和UDP属于传输层协议

TCP和UDP属于传输层协议。其中TCP提供IP环境下的数据可靠传输,它事先为要发送的数据开辟好连接通道(三次握手),然后再进行数据发送;而UDP则不为IP提供可靠性,一般用于实时的视频流传输,像rtp、r…

程序员如何利用空闲时间挣零花钱

一: 私活 作为一名程序员,在上班之余,我们有大把的时间,不能浪费,这些时间其实都是可以用来挖掘自己潜在的创造力,今天要讨论的话题就是,程序员如何利用空余时间挣零花钱?比如说周末…

python中api_通过Python中的API查找相关的工作技能

python中api工作技能世界 (The World of Job Skills) So you want to figure out where your skills fit into today’s job market. Maybe you’re just curious to see a comprehensive constellation of job skills, clean and standardized. Or you need a taxonomy of ski…

欺诈行为识别_使用R(编程)识别欺诈性的招聘广告

欺诈行为识别背景 (Background) Online recruitment fraud (ORF) is a form of malicious behaviour that aims to inflict loss of privacy, economic damage or harm the reputation of the stakeholders via fraudulent job advertisements.在线招聘欺诈(ORF)是一种恶意行为…

c语言实验四报告,湖北理工学院14本科C语言实验报告实验四数组

湖北理工学院14本科C语言实验报告实验四 数组.doc实验四 数 组实验课程名C语言程序设计专业班级 14电气工程2班 学号 201440210237 姓名 熊帆 实验时间 5.12-5.26 实验地点 K4-208 指导教师 祁文青 一、实验目的和要求1. 掌握一维数组和二维数组的定义、赋值和输入输出的方法&a…

rabbitmq channel参数详解【转】

1、Channel 1.1 channel.exchangeDeclare(): type:有direct、fanout、topic三种durable:true、false true:服务器重启会保留下来Exchange。警告:仅设置此选项,不代表消息持久化。即不保证重启后消息还在。原…

nlp gpt论文_GPT-3:NLP镇的最新动态

nlp gpt论文什么是GPT-3? (What is GPT-3?) The launch of Open AI’s 3rd generation of the pre-trained language model, GPT-3 (Generative Pre-training Transformer) has got the data science fraternity buzzing with excitement!Open AI的第三代预训练语言…

真实不装| 阿里巴巴新人上路指北

新手上路,总想听听前辈们分享他们走过的路。橙子选取了阿里巴巴合伙人逍遥子(阿里巴巴集团CEO) 、Eric(蚂蚁金服董事长兼CEO)、Judy(阿里巴巴集团CPO)的几段分享,他们是如何看待职场…

小程序学习总结

上个周末抽空了解了一下小程序,现在将所学所感记录以便日后翻看;需要指出的是我就粗略过了下小程序的api了解了下小程序的开发流程以及工具的使用,然后写了一个小程序的demo;在我看来,如果有前端基础学习小程序无异于锦上添花了,而我这个三年的码农虽也写过不少前端代码但离专业…

uber 数据可视化_使用R探索您在Uber上的活动:如何分析和可视化您的个人数据历史记录

uber 数据可视化Perhaps, dear reader, you are too young to remember that before, the only way to request a particular transport service such as a taxi was to raise a hand to make a signal to an available driver, who upon seeing you would stop if he was not …

java B2B2C springmvc mybatis电子商城系统(四)Ribbon

2019独角兽企业重金招聘Python工程师标准>>> 一:Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如…

基于plotly数据可视化_[Plotly + Datashader]可视化大型地理空间数据集

基于plotly数据可视化简介(我们将创建的内容): (Introduction (what we’ll create):) Unlike the previous tutorials in this map-based visualization series, we will be dealing with a very large dataset in this tutorial (about 2GB of lat, lon coordinat…

Centos用户和用户组管理

inux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统。1、添加新的用户账号使用useradd命令,其语法如下:useradd 选项 用户名-…

划痕实验 迁移面积自动统计_从Jupyter迁移到合作实验室

划痕实验 迁移面积自动统计If you want to use Google Colaboratory to perform your data analysis, for building data pipelines and data visualizations, here is the beginners’ guide to migrate from one tool to the other.如果您想使用Google Colaboratory进行数据分…

数据开放 数据集_除开放式清洗之外:叙述是开放数据门户的未来吗?

数据开放 数据集There is growing consensus in the open data community that the mere release of open data — that is data that can be freely accessed, remixed, and redistributed — is not enough to realize the full potential of openness. Successful open data…

ios android 交互 区别,很多人不承认:iOS的返回交互,对比Android就是反人类。

宁之的奥义2020-09-21 10:54:39点灭只看此人举报给你解答:美国人都是左撇子,所以他们很方便🐶给你解答:美国人都是左撇子,所以他们很方便🐶亮了(504)回复查看评论(19)回忆的褶皱楼主2020-09-21 11:01:01点灭…