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()'方法导入数据。 您需要将纬度和经度列设置为浮点类型,以便稍后我们可以将其转换为网络墨卡托单位。 无论您选择定义气泡图半径的任何列,都应为int或float类型。 在此演示中,它是“ 死亡人数”列。
初始化地图对象 (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!
一旦情节出现在窗口中,请尝试将鼠标悬停在其上方! 将鼠标悬停在地图数据上时,除了提供提示性的工具提示外,您还应该看到地图颜色发生变化。 尝试单击图例中的条目以打开或关闭地图图层!
为什么起作用? (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())'方法在仪表板上同时显示两个地图。 欣赏你的工作! 尝试将鼠标悬停在地图条目上以获取更多信息,然后单击图例以过滤显示。 而已!
摘要 (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,一经查实,立即删除!