有没有办法在 Bokeh 散点图上绘制图像而不是圆点?

问题描述 投票:0回答:0

我正在绘制一些足球射门数据。我有每个镜头的位置(根据 x、y 坐标),数据库包含 4 种不同类型的镜头。在我下面的代码中,我使用了 4 种不同的颜色来区分它们。但是,我有 4 个不同的图标在线显示,而不是颜色,对应于不同的镜头类型。我知道我需要图像字形来实现这一点,但在尝试了一些想法后我被卡住了。有什么办法可以做到这一点?请谢谢!

这是我的代码

from bokeh.models import Range1d, ColumnDataSource,  Select, HoverTool, CheckboxGroup, CategoricalColorMapper, Div
from bokeh.models.glyphs import ImageURL
from bokeh.plotting import figure, show
from bokeh.layouts import column, row
from bokeh.io import curdoc
import pandas as pd
import sqlite3

#region Plot Setup ---------------------------------------------------------
connection = sqlite3.connect("MyDatabase.db")
query = "SELECT x_pos, y_pos, player, outcome, xG, origin, opponent, time, game FROM shotsTaken"
data = pd.read_sql_query(query, connection)
dataSource = ColumnDataSource(data)

hover = HoverTool(tooltips=[
    ("Player", "@player"),
    ("Outcome", "@outcome"),
    ("xG", "@xG"),
    ("Source", "@origin"),
    ("Against", "@opponent"),
    ("Time", "@time"),
    ("Game", "@game"),
    ("Competition", "@competition")
])

plot = figure(
    width = 900, #1500
    height = 712, #1186
    x_range = Range1d(start=0, end=100),
    y_range = Range1d(start=0, end=100),
    tools = [hover, 'reset'],
    toolbar_location="above"
)

# EXPERIMENT ------------------------------------

goalSource = ColumnDataSource(data=dict(
    x=dataSource.data['x_pos'], # set this using the shot location data
    y=dataSource.data['y_pos'], #
    goalURL=["https://postimg.cc/yDdcCMZ3"]
))

goalIcon = ImageURL(url="goalURL", x="x", y="y", w=20, h=20, anchor="center")
plot.add_glyph(goalSource, goalIcon)

# saveSource = ColumnDataSource(data=dict(
#     x=[], # set this later when adding the glyph
#     y=[], # set this later when adding the glyph
#     url=["https://postimg.cc/TK4mdtV7"]
# ))

# wideSource = ColumnDataSource(data=dict(
#     x=[], # set this later when adding the glyph
#     y=[], # set this later when adding the glyph
#     url=["https://postimg.cc/94w9ry1W"]
# ))

# postSource = ColumnDataSource(data=dict(
#     x=[], # set this later when adding
#     y=[], # set this later when adding
#     url=["https://postimg.cc/94w9ry1W"]
# ))

# -----------------------------------------------

backgroundSource = ColumnDataSource(data=dict(url=["https://i.postimg.cc/gJtbqRCf/background.png"]))
background = ImageURL(url="url", x="x_pos", y="y_pos", w=20, h=20, anchor="center")
plot.add_glyph(dataSource, background)

plot.image_url(
    x=0.0, 
    y=100, 
    h=100, 
    w=100,
    url="url",
    source=backgroundSource,
    level='image'
)

color_mapper = CategoricalColorMapper(
    factors=['Goal', 'Save', 'Wide', 'Post'],
    palette=['green', 'red', 'blue', 'white']
)

plot.scatter(
    "x_pos",
    "y_pos",
    size=5, 
    source=dataSource, 
    line_color="black", 
    color = {'field': 'outcome', 'transform': color_mapper},
    alpha=0.5
)
python image bokeh scatter-plot glyphicons
© www.soinside.com 2019 - 2024. All rights reserved.