简体   繁体   English

在python中使用散景和悬停工具

[英]Using bokeh and hover tool in python

I need to make an interactive dot plot in bokeh using Python.我需要使用 Python 在散景中制作交互式点图。 Basically, I need to make a map using the latitudes and longitudes to show people that own these different pets.基本上,我需要使用纬度和经度制作一张地图来显示拥有这些不同宠物的人。

The type variable has either dog, cat, bird, lizard, or other.类型变量有狗、猫、鸟、蜥蜴或其他。 Dots that are blue must show dog owners, red dots show cat owners, green show lizard owners, and pink show bird owners.蓝色的点必须显示狗主人,红色点显示猫主人,绿色显示蜥蜴主人,粉红色显示鸟主人。 I need to delete the 'other' values from the type variable because I don't want that on the map.我需要从类型变量中删除“其他”值,因为我不希望它出现在地图上。

I also need help with the hover tool because when I scroll over the dots, I want to see the type of pet and latitude/longitude of that dot.我还需要有关悬停工具的帮助,因为当我滚动这些点时,我想查看宠物的类型以及该点的纬度/经度。

I am a beginner, so I have only figured out how to import the excel file to python.我是初学者,所以我只知道如何将excel文件导入python。

Thanks!谢谢!

The data looks like this:数据如下所示:

Type Latitude Longitude 

Dog 41.9595 82.494997

Cat 41.4388 82.493585

And etc......等等......

I wish I could attach the data set, but I can't seem to here.我希望我能附上数据集,但我似乎不能在这里。 However, I don't need the exact results.但是,我不需要确切的结果。 Just an idea of the code to use to do this.只是用于执行此操作的代码的想法。

So far, this is what I have到目前为止,这就是我所拥有的

import pandas as pd
Pet_Data = pd.read_csv('PetMap.csv',sep=',')
Pet_Data.head()

Welcome to the community!欢迎来到社区! Since you are fairly new it is a good idea to go through some tutorials.由于您是新手,因此最好阅读一些教程。 I myself have recently learned Bokeh for some really cool interactive charts.我自己最近为一些非常酷的交互式图表学习了 Bokeh。

I can show you a few tutorial links that helped me understand how to use Bokeh.我可以向您展示一些帮助我了解如何使用 Bokeh 的教程链接。 I'll assume you're a complete beginner to the Python universe.我假设您是 Python 世界的完全初学者。

  1. I would go through this quickstart tutorial to get a feel for how Bokeh works and getting comfortable navigating the docs我会通过这个快速入门教程来了解 Bokeh 是如何工作的,以及如何轻松浏览文档

HoveringTooltips is fairly simple to grasp once you understand what it needs.一旦您了解了HoveringTooltips 的需要,它就非常容易掌握。
That being said, I'll leave snippets of code for what would go into the tooltip argument within a Bokeh figure .话虽如此,我将留下代码片段,用于在 Bokeh figure 中进入 tooltip 参数。


Import necessities and print Pet_data进口生活必需品并打印Pet_data

import pandas as pd
from bokeh.plotting import figure, show

# make sure we know what our table looks like
print(Pet_data)

    Type    Latitude    Longitude
0   Dog     41.9595     82.494997
1   Cat     41.4388     82.493585

tooltip works something like this:工具提示的工作方式如下:

  1. Build a list of tuples( TOOLTIP ) so hover tooltip will know what to access in your Pet_data构建一个元组列表( TOOLTIP ),以便悬停工具提示将知道在您的Pet_data访问什么
  2. Initiate figure with TOOLTIP as argument, and store as variable pTOOLTIP为参数启动figure ,并存储为变量p
  3. Add circle s to p according to Latitude , Longitude根据LatitudeLongitudecircle s 添加到p
  4. show() to print the chart show()打印图表
# 1
TOOLTIPS = [("type", "@Type"),       # this accesses 'Type' column
            ("lat", "@Latitude"),    # this accesses 'Latitude' column
            ("lat", "@Longitude")]   # this accesses 'Longitude' column

# 2
p = figure(tooltips=TOOLTIPS)        # initiate your figure and add TOOLTIP

# 3
p.circle(x='Latitude',               # circles on x
         y='Longitude',                and on y
         source=Pet_Data             # points to Pet_data df
         size=40)                    # simple circle size argument

# 4
show(p)                              # prints your figure

Here is a print-screen with how the hover would look like:这是一个打印屏幕,显示悬停的样子: 示例 od Pet_data 悬停工具提示


Now, granted this isn't a map like you would probably want since you have latitude and longitude values, but I just wanted to give you a simple example of how the hover tooltip works.现在,虽然这不是您可能想要的地图,因为您有纬度和经度值,但我只想给您一个简单的示例,说明悬停工具提示的工作原理。

Hope it was helpful and good luck on your programming journey!希望它对您的编程之旅有所帮助并祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM