在散景中更改网格位置

Changing grid position in Bokeh

我正在尝试制作一个 table 来表示基于计数的分类值之间的关系。这是我当前的代码:

source = ColumnDataSource(df)
mapper = LinearColorMapper(palette=brewer['YlGnBu'][4], low=df['count'].min(), high=df['count'].max())
tooltips = [
    ("Resource", "@org:resoure"),
    ("Activity", "@concept:name"),
    ("Count", "@count"),
]
p = figure(plot_width=df['org:resource'].nunique()*180, plot_height=df['concept:name'].nunique()*100, title="US Unemployment 1948—2016",
           y_range=df['concept:name'].unique(), x_range=df['org:resource'].unique(),x_axis_location="above", tooltips=tooltips)

p.rect(y="concept:name", x="org:resource", width=1, height=1, source=source,
       line_color=None, fill_color=transform('count', mapper))

color_bar = ColorBar(color_mapper=mapper, location=(0, 0),
                     ticker=BasicTicker(),
                     formatter=PrintfTickFormatter(format="%d%%"))

p.add_layout(color_bar, 'right')

p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_text_font_size = "13px"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = 0

output_notebook()
show(p)

但是,此代码生成的网格不适合分类值。如何向下移动网格?

current visual produced by code

您可以使用 FixedTicker 实现此目的。

这是一个非常基本的示例,其中使用了 p.rect()

old new

代码非常相似,除了在新版本中使用我提到的 FixedTicker 在两个轴上将刻度移动 0.5

from bokeh.plotting import figure, output_file, show

data = [1, 5]
plot = figure(plot_width=300, plot_height=300)
plot.rect(x=data, y=data, width=1, height=1, color="#CAB2D6")

show(plot)

from bokeh.plotting import figure, output_file, show
from bokeh.models import FixedTicker

data = [1, 5]
plot = figure(plot_width=300, plot_height=300)
plot.rect(x=data, y=data, width=1, height=1, color="#CAB2D6")

_max = max(data)
plot.xaxis.ticker = FixedTicker(ticks=[x+0.5 for x in range(_max+1)])
plot.yaxis.ticker = FixedTicker(ticks=[x+0.5 for x in range(_max+1)])

show(plot)