Python Bokeh:在rect中更新空FactorRange

Python Bokeh:在rect中更新空FactorRange,python,bokeh,Python,Bokeh,我想展示博克的相关热图。我正在使一个空的数字最小化: p2 = figure(x_range=[], y_range=[], plot_width=1200,plot_height=1200) 选择并加载数据源后(单击按钮),我会 热图显示,但x_range.factors和y_range.factors保持空白 如果我使用以下命令初始化图p2: test = pd.read_csv("Heatmap.csv") source_heat_map.data = test.to_dict('lis

我想展示博克的相关热图。我正在使一个空的数字最小化:

p2 = figure(x_range=[], y_range=[], plot_width=1200,plot_height=1200)
选择并加载数据源后(单击按钮),我会

热图显示,但x_range.factors和y_range.factors保持空白

如果我使用以下命令初始化图p2:

test = pd.read_csv("Heatmap.csv")
source_heat_map.data = test.to_dict('list')
name_a = test.columns[0]
name_b = test.columns[1]
p2 = figure(x_range=b, y_range=a, plot_width=1200,plot_height=1200)
p2.rect(
        x=name_b,
        y=name_a,
        width=1,
        height=1,
        source=source_heat_map,
        line_color=None,
        fill_color=bokeh_transform('value', mapper)
    )
然后选择另一个数据源并单击按钮,它将工作,x_range.factors和y_range.factors将得到更新。我要怎么做才能更新空的工厂安排

编辑:

下面是一个最低限度的工作示例。如果将
If True:
更改为
If False:
并初始化
p2=figure(x\u range=[],y\u range=[],plot\u width=1200,plot\u height=1200)
x\u range.factors和y\u range.factors作为空列表,它将不再工作

from bokeh.io import curdoc
from bokeh.layouts import column,row, widgetbox, Spacer
from bokeh.models import ColumnDataSource, Paragraph, LinearColorMapper, ColorBar, BasicTicker, TapTool, CustomJS,BoxSelectTool, Rect, FactorRange
from bokeh.models.widgets import Slider, TextInput, Div, Button, Dropdown, TableColumn, DataTable, CheckboxButtonGroup
from bokeh.models.annotations import Title
from bokeh.plotting import figure,show
from bokeh.client import push_session

from bokeh.transform import transform as bokeh_transform
from bokeh import events

from math import pi
import pandas as pd


def compute_corr():
    global matches
    corr = pd.DataFrame.from_dict({'TABLE1': {0: 'G', 1: 'L', 2: 'M', 3: 'N', 4: 'H', 5: 'T'}, 'value': {0: 1.0, 1: 0.5493847383480001, 2: 0.14649061756799993, 3: 0.39124820471999999, 4: 0.325265107675299999, 5: 0.668616128290099998}, 'TABLE2': {0: 'G', 1: 'G', 2: 'G', 3: 'G', 4: 'G', 5: 'G'}})

    p2.x_range.factors = corr["TABLE2"].unique().tolist()
    p2.y_range.factors = corr["TABLE1"].unique().tolist()
    source_heat_map.data = corr.to_dict('list')
    p2.rect(
            x="TABLE2",
            y="TABLE1",
            width=1,
            height=1,
            source=source_heat_map,
            line_color=None,
            fill_color=bokeh_transform('value', mapper)
        )

# You can use your own palette here
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']


source_heat_map = ColumnDataSource(data = {})


b1 = Button(label="create", width=200, height=100)
b1.on_click(compute_corr)

mapper = LinearColorMapper(
        palette=colors, low=0, high=1)

if True:
    test = pd.DataFrame.from_dict({'TABLE1': {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F'}, 'value': {0: 1.0, 1: 0.8493847383480001, 2: 0.84649061756799993, 3: 0.89124820471999999, 4: 0.15265107675299999, 5: 0.068616128290099998}, 'TABLE2': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A', 5: 'A'}})
    source_heat_map.data = test.to_dict('list')
    name_a = 'TABLE1'
    name_b = 'TABLE2'
    a = list(set(test["TABLE1"].values))
    b = list(set(test["TABLE2"].values))
    print a,b
    p2 = figure(x_range=b, y_range=a, plot_width=1200,plot_height=1200)
    p2.rect(
            x=name_b,
            y=name_a,
            width=1,
            height=1,
            source=source_heat_map,
            line_color=None,
            fill_color=bokeh_transform('value', mapper)
        )
else:
    p2 = figure(x_range=[], y_range=[], plot_width=1200,plot_height=1200)

color_bar = ColorBar(
            color_mapper=mapper,
            location=(0, 0),
            ticker=BasicTicker(desired_num_ticks=len(colors)))

p2.add_layout(color_bar, 'right')
p2.toolbar.logo = None
p2.toolbar_location = None
p2.xaxis.major_label_orientation = pi / 3

curdoc().add_root(row(b1,p2))
curdoc().title = "Correlations"

升级到Bokeh版本0.13.0解决了问题。

升级到Bokeh版本0.13.0解决了问题。

没有足够的信息继续。如果没有一个完整的最小复制程序来运行和调查,这可能是您的代码中的错误或使用错误,这是不可能说的。
if False
版本在
0.13
1.0dev
中的工作与我预期的一样。“x_范围系数和y_范围系数保持空白”的确切含义是什么?轴将更新,新因子将显示在绘图上。我不确定你还期望发生什么。也许在以前的版本中有一个bug。如果您使用的是较旧的版本,则在请求帮助时应始终说明这一重要信息。我运行的是版本0.12.10。升级到0.13.0版,并且可以正常工作。谢谢没有足够的信息继续下去。如果没有一个完整的最小复制程序来运行和调查,这可能是您的代码中的错误或使用错误,这是不可能说的。
if False
版本在
0.13
1.0dev
中的工作与我预期的一样。“x_范围系数和y_范围系数保持空白”的确切含义是什么?轴将更新,新因子将显示在绘图上。我不确定你还期望发生什么。也许在以前的版本中有一个bug。如果您使用的是较旧的版本,则在请求帮助时应始终说明这一重要信息。我运行的是版本0.12.10。升级到0.13.0版,并且可以正常工作。谢谢
from bokeh.io import curdoc
from bokeh.layouts import column,row, widgetbox, Spacer
from bokeh.models import ColumnDataSource, Paragraph, LinearColorMapper, ColorBar, BasicTicker, TapTool, CustomJS,BoxSelectTool, Rect, FactorRange
from bokeh.models.widgets import Slider, TextInput, Div, Button, Dropdown, TableColumn, DataTable, CheckboxButtonGroup
from bokeh.models.annotations import Title
from bokeh.plotting import figure,show
from bokeh.client import push_session

from bokeh.transform import transform as bokeh_transform
from bokeh import events

from math import pi
import pandas as pd


def compute_corr():
    global matches
    corr = pd.DataFrame.from_dict({'TABLE1': {0: 'G', 1: 'L', 2: 'M', 3: 'N', 4: 'H', 5: 'T'}, 'value': {0: 1.0, 1: 0.5493847383480001, 2: 0.14649061756799993, 3: 0.39124820471999999, 4: 0.325265107675299999, 5: 0.668616128290099998}, 'TABLE2': {0: 'G', 1: 'G', 2: 'G', 3: 'G', 4: 'G', 5: 'G'}})

    p2.x_range.factors = corr["TABLE2"].unique().tolist()
    p2.y_range.factors = corr["TABLE1"].unique().tolist()
    source_heat_map.data = corr.to_dict('list')
    p2.rect(
            x="TABLE2",
            y="TABLE1",
            width=1,
            height=1,
            source=source_heat_map,
            line_color=None,
            fill_color=bokeh_transform('value', mapper)
        )

# You can use your own palette here
colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']


source_heat_map = ColumnDataSource(data = {})


b1 = Button(label="create", width=200, height=100)
b1.on_click(compute_corr)

mapper = LinearColorMapper(
        palette=colors, low=0, high=1)

if True:
    test = pd.DataFrame.from_dict({'TABLE1': {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F'}, 'value': {0: 1.0, 1: 0.8493847383480001, 2: 0.84649061756799993, 3: 0.89124820471999999, 4: 0.15265107675299999, 5: 0.068616128290099998}, 'TABLE2': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A', 5: 'A'}})
    source_heat_map.data = test.to_dict('list')
    name_a = 'TABLE1'
    name_b = 'TABLE2'
    a = list(set(test["TABLE1"].values))
    b = list(set(test["TABLE2"].values))
    print a,b
    p2 = figure(x_range=b, y_range=a, plot_width=1200,plot_height=1200)
    p2.rect(
            x=name_b,
            y=name_a,
            width=1,
            height=1,
            source=source_heat_map,
            line_color=None,
            fill_color=bokeh_transform('value', mapper)
        )
else:
    p2 = figure(x_range=[], y_range=[], plot_width=1200,plot_height=1200)

color_bar = ColorBar(
            color_mapper=mapper,
            location=(0, 0),
            ticker=BasicTicker(desired_num_ticks=len(colors)))

p2.add_layout(color_bar, 'right')
p2.toolbar.logo = None
p2.toolbar_location = None
p2.xaxis.major_label_orientation = pi / 3

curdoc().add_root(row(b1,p2))
curdoc().title = "Correlations"