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

Using bokeh and hover tool in python

我需要使用 Python 在散景中制作交互式点图。基本上,我需要使用纬度和经度制作一张地图,以显示拥有这些不同宠物的人。

类型变量有狗、猫、鸟、蜥蜴或其他。蓝色点表示狗主人,红色点表示猫主人,绿色表示蜥蜴主人,粉色表示鸟主人。我需要从类型变量中删除 'other' 值,因为我不想在地图上显示它。

我还需要悬停工具方面的帮助,因为当我滚动点时,我想查看宠物的类型和那个点的 latitude/longitude。

我是初学者,所以我只知道如何将excel文件导入python。

谢谢!

数据如下所示:

Type Latitude Longitude 

Dog 41.9595 82.494997

Cat 41.4388 82.493585

等等……

我希望我可以附上数据集,但我这里好像做不到。但是,我不需要确切的结果。只是用于执行此操作的代码的想法。

到目前为止,这就是我所拥有的

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

欢迎来到社区!由于您是相当新的,因此最好阅读一些教程。我本人最近为了一些非常酷的交互式图表而学习了 Bokeh。

我可以向您展示一些帮助我了解如何使用 Bokeh 的教程链接。我假设您是 Python 宇宙的初学者。

  1. 我会通过这个 quickstart tutorial 来了解 Bokeh 的工作原理并轻松浏览文档

HoveringTooltips 一旦您了解了它的需求,就相当容易掌握。
话虽这么说,我将留下一些代码片段,用于在 Bokeh figure.

中进入工具提示参数。

导入必需品并打印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

工具提示是这样工作的:

  1. 建立一个元组列表(TOOLTIP),这样悬停工具提示就会知道要做什么 在您的 Pet_data
  2. 中访问
  3. TOOLTIP作为参数启动figure,并存储为变量p
  4. 根据LatitudeLongitude
  5. circle添加到p
  6. 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

这是一个显示悬停效果的打印屏幕:


现在,假设这不是您可能想要的地图,因为您有纬度和经度值,但我只是想给您一个简单的示例,说明悬停工具提示的工作原理。

希望对您有所帮助,祝您编程之旅顺利!