Python 向点边界添加标签

Python 向点边界添加标签,python,pandas,bokeh,Python,Pandas,Bokeh,根据他们的文档,我有以下信息: album_names = ['Ctrl', 'Ctrl', 'Z', 'Ctrl', 'Ctrl', 'Z', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Z'] valence = [0.37, 0.598, 0.481, 0.166, 0.413, 0.0798, 0.54

根据他们的文档,我有以下信息:

album_names = ['Ctrl', 'Ctrl', 'Z', 'Ctrl', 'Ctrl', 'Z', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Ctrl', 'Z']
valence = [0.37, 0.598, 0.481, 0.166, 0.413, 0.0798, 0.549, 0.332, 0.348, 0.335, 0.355, 0.22, 0.433, 0.158, 0.357, 0.134, 0.367, 0.237, 0.248, 0.239, 0.535, 0.432, 0.505, 0.142]
energy = [0.579, 0.686, 0.551, 0.367, 0.61, 0.475, 0.488, 0.525, 0.534, 0.517, 0.56, 0.342, 0.688, 0.505, 0.551, 0.63, 0.71, 0.453, 0.518, 0.708, 0.463, 0.684, 0.296, 0.576]`

df = pd.DataFrame([album_names, energy, valence]).T
df.columns = ['album_name', 'energy', 'valence']
我想用bokeh做一个散点图,x轴上有价,y轴上有能。另外,当您将鼠标悬停在每个点上时,我希望它表示它是
album\u name
的值。点的颜色基于相册名称

我尝试了以下方法:

from bokeh.models import ColumnDataSource, Range1d, LabelSet, Label
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.palettes import brewer

source = ColumnDataSource(data=dict(valence=valence,
                                    energy=energy,
                                    names=album_names))

p = figure()
p.scatter(x='valence', y='energy', size=8, source=source)
labels = LabelSet(x='valence', y='energy', text='names',
                  level='glyph', x_offset=5, y_offset=5,
                  source=source, render_mode='canvas')

p.add_layout(labels)
show(p)
但当您将鼠标悬停在该点上时,这不会显示唱片集名称。它修复了该点旁边的唱片集名称。如果能够帮助您仅在将鼠标悬停在该点上并根据album_name的值更改颜色时显示album_name,我们将不胜感激

注意:它将类似于下面 试试这个:

from bokeh.io import show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure

album_names = [...]
valence = [...]
energy = [...]

source = ColumnDataSource(data=dict(valence=valence,
                                    energy=energy,
                                    names=album_names))

p = figure(tools=[HoverTool(tooltips=[('Album Name', '@names'),
                                      ('Valence', '@valence'),
                                      ('Energy', '@energy')])])
p.scatter(x='valence', y='energy', size=8, source=source)
show(p)
所有工具提示都由
鼠标悬停工具显示,因此您必须添加并配置它以显示所需内容。

您可以在其中找到更多详细信息。

选中此选项,它将完成以下操作:

from bokeh.plotting import figure, ColumnDataSource, output_notebook, show
from bokeh.models import HoverTool, WheelZoomTool, PanTool, BoxZoomTool, ResetTool, TapTool, SaveTool
from bokeh.palettes import brewer

output_notebook()

#preprocessing the data with column album_name
category = 'album_name'

category_items = df[category].unique()
#selecting the colors for each unique category in album_name
palette = brewer['Set2'][len(category_items) + 1]
#mapping each category with a color of Set2
colormap = dict(zip(category_items, palette))
#making a color column based on album_name
df['color'] = df[category].map(colormap)

title = "Album_names"
#feeding data into ColumnDataSource
source = ColumnDataSource(df)
#Editing the hover that need to displayed while hovering
hover = HoverTool(tooltips=[('Album_name', '@album_name')])
#tools that are need to explote data
tools = [hover, WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool()]

#finally making figure with scatter plot
p = figure(tools=tools,title=title,plot_width=700,plot_height=400,toolbar_location='right',toolbar_sticky=False, )
p.scatter(x='valence',y='energy',source=source,size=10,color='color',legend=category)

#displaying the graph
show(p)
输出将显示为相册名称Ctrl
输出将显示为相册_name Z所示

感谢编辑@user4261201。不知道如何正确格式化代码。如果您直接使用该文档示例,您将无法获得相册名称的不同颜色。检查我的溶液,它给出不同的颜色@Eugene是的,问题的标题不包括点的着色,所以我忽略了它。看起来很棒!我假设
brewer['Set2'][len(category_items)+1]
是因为brewer['Set2']至少有三个选项?@iwtbid这是为了选择要在图表中显示的颜色。如果它有助于自由接受/投票表决解决方案。非常感谢。