使散景图散点的颜色和标记取决于数据框值

OD1*_*995 1 python pandas bokeh

我一直在玩散景,以获得交互式散点图、工具提示和交互式图例等。

目前,我可以使用绘图后面的 Pandas 数据框中列的值来设置点的颜色。但是我想知道是否可以使用数据框中的另一列设置标记类型(菱形、圆形、方形等)?

我很欣赏这意味着您需要一个双重图例,但希望这不会成为太大的问题。

big*_*dot 5

从 Bokeh 1.0 开始,这可以使用marker_mapCDS 过滤器来完成:

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers
from bokeh.transform import factor_cmap, factor_mark

SPECIES = ['setosa', 'versicolor', 'virginica']
MARKERS = ['hex', 'circle_x', 'triangle']

p = figure(title = "Iris Morphology", background_fill_color="#fafafa")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Sepal Width'

p.scatter("petal_length", "sepal_width", source=flowers, legend="species", 
          fill_alpha=0.4, size=12,
          marker=factor_mark('species', MARKERS, SPECIES),
          color=factor_cmap('species', 'Category10_3', SPECIES))

show(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


旧答案

对于 Bokeh,0.13.0它仍然是一个开放的功能请求,可以直接从数据中参数化标记类型:创建包含所有标记的标记类,并允许从数据中指定特定的标记类型#5884

在实现之前,最好的办法是利用CDSView模型在多个字形方法中拆分单个数据集:

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CDSView, GroupFilter
from bokeh.sampledata.iris import flowers

source = ColumnDataSource(flowers)

setosa = CDSView(source=source, filters=[GroupFilter(column_name='species', group='setosa')])
versicolor = CDSView(source=source, filters=[GroupFilter(column_name='species', group='versicolor')])
virginica = CDSView(source=source, filters=[GroupFilter(column_name='species', group='virginica')])

p = figure()

p.circle(x='petal_length', y='petal_width', source=source, view=setosa,
         size=10, color='red', alpha=0.6, legend='setosa')

p.square(x='petal_length', y='petal_width', source=source, view=versicolor,
         size=10, color='green', alpha=0.6, legend='versicolor')

p.triangle(x='petal_length', y='petal_width', source=source, view=virginica,
           size=10, color='blue', alpha=0.6, legend='virginica')

p.legend.location = "top_left"
show(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明