Bokeh中的factor_cmap未正确应用于分类图表。

huangapple go评论52阅读模式
英文:

factor_cmap in Bokeh is not being applied correctly for categorical chart

问题

我刚刚开始学习Bokeh,我试图创建一个带有抖动的条带图,以显示7个类别上的变化,其中x轴是错误,y轴是类别。我希望每个类别都有不同的颜色。

似乎在bokeh.transform下应该使用factor_cmap:
https://docs.bokeh.org/en/latest/docs/reference/transform.html

代码运行没有错误,但颜色从未应用(图表以默认颜色绘制,即蓝色)。

df = pd.read_csv(r'E:\Bokeh\Charting_vars.csv')
category = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
source = ColumnDataSource(df)
p = figure(width=800, height=800, y_range=category, title="Variation across 7 samples")

p.circle(
    x='Error',
    y=jitter('Category', width=0.6, range=p.y_range),
    source=source,
    fill_color=factor_cmap('Category', palette="Spectral7", factors=category)
)
p.ygrid.grid_line_color = None

show(p)

我漏掉了什么?

英文:

I just started learning Bokeh and I am trying to create a strip plot if you will with jitter to show variation over 7 categories where x axis is the error and y-axis is the category. I would like for each category to be a different color.

Seems like factor_cmap is what I should be using under bokeh.transform:
https://docs.bokeh.org/en/latest/docs/reference/transform.html

The code runs without errors but the colors are never applied (the graph draws with default colors which is blue).

df = pd.read_csv(r'E:\Bokeh\Charting_vars.csv')
category = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
source = ColumnDataSource(df)
p = figure(width=800, height=800, y_range=category, title="Variation across 7 samples")

p.circle(
    x='Error',
    y=jitter('Category', width=0.6, range=p.y_range),
    source=source,
    fill_color=factor_cmap('Category', palette="Spectral7", factors=category)
)
p.ygrid.grid_line_color = None

show(p)

What am I missing?

答案1

得分: 1

您的代码看起来很好,就我所看到的而言。也许有拼写错误。您能否请向我们展示您的数据框?希望我们可以重现您的问题。

为了帮助您,我从官方文档中修改了scatter_jitter示例。我只是添加了factor_cmap

示例代码

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
from bokeh.sampledata.commits import data
from bokeh.transform import jitter, factor_cmap
output_notebook()

DAYS = ['Sun', 'Sat', 'Fri', 'Thu', 'Wed', 'Tue', 'Mon']

source = ColumnDataSource(data)

p = figure(
    width=800,
    height=300,
    y_range=DAYS,
    x_axis_type='datetime',
    title="Commits by Time of Day (US/Central) 2012-2016"
)

p.scatter(
    x='time',
    y=jitter('day', width=0.6, range=p.y_range),
    source=source,
    alpha=0.3,
    fill_color=factor_cmap('day', palette='Spectral7', factors=DAYS)
)

p.xaxis.formatter.days = '%Hh'
p.x_range.range_padding = 0
p.ygrid.grid_line_color = None

show(p)

输出

Bokeh中的factor_cmap未正确应用于分类图表。

这是使用 bokeh 2.4.3 创建的。

英文:

Your code looks fine as far as I can tell. Maybe there is a typo somewhere. Could you please show us your DataFrame? Hopefully we can reproduce your problem.

To help you I modified the scatter_jitter example from the official documentation. I just added factor_cmap.

Example Code

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
from bokeh.sampledata.commits import data
from bokeh.transform import jitter, factor_cmap
output_notebook()

DAYS = ['Sun', 'Sat', 'Fri', 'Thu', 'Wed', 'Tue', 'Mon']

source = ColumnDataSource(data)

p = figure(
    width=800,
    height=300,
    y_range=DAYS,
    x_axis_type='datetime',
    title="Commits by Time of Day (US/Central) 2012-2016"
)

p.scatter(
    x='time',
    y=jitter('day', width=0.6, range=p.y_range),
    source=source,
    alpha=0.3,
    fill_color=factor_cmap('day', palette='Spectral7', factors=DAYS)
)

p.xaxis.formatter.days = '%Hh'
p.x_range.range_padding = 0
p.ygrid.grid_line_color = None

show(p)

Output

Bokeh中的factor_cmap未正确应用于分类图表。

This was created with bokeh 2.4.3.

huangapple
  • 本文由 发表于 2023年2月8日 12:49:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定