Python 使bokeh图分散点的颜色和标记取决于数据帧值

Python 使bokeh图分散点的颜色和标记取决于数据帧值,python,pandas,bokeh,Python,Pandas,Bokeh,为了得到一个交互式散点图,有工具提示和交互式图例等,我一直在和博克玩 目前,我可以使用绘图后面数据框中的列值设置点的颜色。但是,我想知道是否可以使用数据框中的另一列设置标记类型(菱形、圆形、正方形等) 我很感激这意味着您需要一个双图例,但希望这不会有太大问题。从Bokeh 1.0开始,这可以通过标记地图和CDS过滤器来实现: from bokeh.plotting import figure, show, output_file from bokeh.sampledata.iris import

为了得到一个交互式散点图,有工具提示和交互式图例等,我一直在和博克玩

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


我很感激这意味着您需要一个双图例,但希望这不会有太大问题。

从Bokeh 1.0开始,这可以通过
标记地图和CDS过滤器来实现:

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)


旧答案

从Bokeh
0.13.0
开始,标记类型直接从数据参数化仍然是一个开放的特性请求:

在实现此功能之前,最好的办法是利用在多个glyph方法中拆分单个数据集:

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)

从Bokeh 1.0开始,这可以通过
标记映射和CDS过滤器来实现:

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)


旧答案

从Bokeh
0.13.0
开始,标记类型直接从数据参数化仍然是一个开放的特性请求:

在实现此功能之前,最好的办法是利用在多个glyph方法中拆分单个数据集:

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)

谢谢你的帮助@bigreddot。在这个解决方案中,是否可能有不同颜色和形状组合的点?例如,一个点是正方形和蓝色,一个点是三角形和绿色,还有一个点是正方形和绿色。我不确定我是否理解这个问题。上图中的点是颜色和形状的不同组合。在任何情况下,您都可以将上面的
color
值设置为您喜欢的任何颜色。在上面的绘图中,所有的圆都是红色的,所有的正方形都是绿色的,所有的三角形都是蓝色的。是否可能有一个由数据决定标记和形状的绘图,例如,这将导致一些正方形为绿色,而其他正方形为红色?我的猜测是使用p.circle/square/triangleYes的color参数,
color
参数也可以是数据源中一列的名称,您可以用每个标记的颜色值填充该列。或者,
color
可以是颜色映射器或
CustomJSTransform
,可以基于其他列在浏览器中动态地对标记进行颜色映射。文档中有很多例子。谢谢你的帮助@bigreddot。在这个解决方案中,是否可能有不同颜色和形状组合的点?例如,一个点是正方形和蓝色,一个点是三角形和绿色,还有一个点是正方形和绿色。我不确定我是否理解这个问题。上图中的点是颜色和形状的不同组合。在任何情况下,您都可以将上面的
color
值设置为您喜欢的任何颜色。在上面的绘图中,所有的圆都是红色的,所有的正方形都是绿色的,所有的三角形都是蓝色的。是否可能有一个由数据决定标记和形状的绘图,例如,这将导致一些正方形为绿色,而其他正方形为红色?我的猜测是使用p.circle/square/triangleYes的color参数,
color
参数也可以是数据源中一列的名称,您可以用每个标记的颜色值填充该列。或者,
color
可以是颜色映射器或
CustomJSTransform
,可以基于其他列在浏览器中动态地对标记进行颜色映射。文档中有很多例子。