简体   繁体   English

散景散点图中的色点

[英]Color points in scatter plot of Bokeh

I have the following simple pandas.DataFrame : 我有以下简单的pandas.DataFrame

df = pd.DataFrame(
    {
        "journey": ['ch1', 'ch2', 'ch2', 'ch1'],
        "cat": ['a', 'b', 'a', 'c'],
        "kpi1": [1,2,3,4],
        "kpi2": [4,3,2,1]
    }
)

Which I plot as follows: 我的情节如下:

import bokeh.plotting as bpl
import bokeh.models as bmo
bpl.output_notebook()
source = bpl.ColumnDataSource.from_df(df)
hover = bmo.HoverTool(
    tooltips=[
        ("index", "@index"),
        ('journey', '@journey'),
        ("Cat", '@cat')
    ]
)
p = bpl.figure(tools=[hover])

p.scatter(
    'kpi1', 
    'kpi2', source=source)

bpl.show(p)  # open a browser

I am failing to color code the dots according to the cat . 我没有根据cat颜色编码点。 Ultimately, I want to have the first and third point in the same color, and the second and fourth in two more different colors. 最终,我希望第一和第三点在相同的颜色,第二和第四点在两种不同的颜色。

How can I achieve this using Bokeh? 如何使用Bokeh实现这一目标?

Here's a way that avoids manual mapping to some extent. 这是一种在某种程度上避免手动映射的方法。 I recently stumbled on bokeh.palettes at this github issue , as well as CategoricalColorMapper in this issue . 我最近偶然在bokeh.palettes这个问题github上 ,以及CategoricalColorMapper这个问题上 This approach combines them. 这种方法结合了它们。 See the full list of available palettes here and the CategoricalColorMapper details here . 查看可用调色板的完整列表在这里CategoricalColorMapper细节在这里

I had issues getting this to work directly on a pd.DataFrame , and also found it didn't work using your from_df() call. 我有问题直接在pd.DataFrame上工作,并且发现它使用你的from_df()调用from_df() The docs show passing a DataFrame directly, and that worked for me. 文档显示直接传递DataFrame ,这对我DataFrame

import pandas as pd
import bokeh.plotting as bpl
import bokeh.models as bmo
from bokeh.palettes import d3
bpl.output_notebook()


df = pd.DataFrame(
    {
        "journey": ['ch1', 'ch2', 'ch2', 'ch1'],
        "cat": ['a', 'b', 'a', 'c'],
        "kpi1": [1,2,3,4],
        "kpi2": [4,3,2,1]
    }
)
source = bpl.ColumnDataSource(df)

# use whatever palette you want...
palette = d3['Category10'][len(df['cat'].unique())]
color_map = bmo.CategoricalColorMapper(factors=df['cat'].unique(),
                                   palette=palette)

# create figure and plot
p = bpl.figure()
p.scatter(x='kpi1', y='kpi2',
          color={'field': 'cat', 'transform': color_map},
          legend='cat', source=source)
bpl.show(p)

For the sake of completeness, here is the adapted code using low-level chart: 为了完整起见,这里是使用低级图表的改编代码:

import pandas as pd

import bokeh.plotting as bpl
import bokeh.models as bmo
bpl.output_notebook()


df = pd.DataFrame(
    {
        "journey": ['ch1', 'ch2', 'ch2', 'ch1'],
        "cat": ['a', 'b', 'a', 'c'],
        "kpi1": [1,2,3,4],
        "kpi2": [4,3,2,1],
        "color": ['blue', 'red', 'blue', 'green']
    }
)
df

source = bpl.ColumnDataSource.from_df(df)
hover = bmo.HoverTool(
    tooltips=[
        ('journey', '@journey'),
        ("Cat", '@cat')
    ]
)
p = bpl.figure(tools=[hover])

p.scatter(
    'kpi1', 
    'kpi2', source=source, color='color')

bpl.show(p)

Note that the colors are "hard-coded" into the data. 请注意,颜色是“硬编码”到数据中的。

Here is the alternative using high-level chart: 以下是使用高级图表的替代方法:

import pandas as pd

import bokeh.plotting as bpl
import bokeh.charts as bch
bpl.output_notebook()

df = pd.DataFrame(
    {
        "journey": ['ch1', 'ch2', 'ch2', 'ch1'],
        "cat": ['a', 'b', 'a', 'c'],
        "kpi1": [1,2,3,4],
        "kpi2": [4,3,2,1]
    }
)

tooltips=[
        ('journey', '@journey'),
        ("Cat", '@cat')
    ]
scatter = bch.Scatter(df, x='kpi1', y='kpi2',
                      color='cat',
                      legend="top_right",
                      tooltips=tooltips
                     )

bch.show(scatter)

you could use the higher level Scatter like here 你可以像这里一样使用更高级别的Scatter

or provide a color column to the ColumnDataSource and reference it in your p.scatter(..., color='color_column_label') 或者为ColumnDataSource提供一个颜色列并在p.scatter(..., color='color_column_label')引用它p.scatter(..., color='color_column_label')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM