散景:在rect中更新空的FactorRange

问题描述 投票:0回答:1

我想在散景中显示相关热图。我正在搞一个空图:

p2 = figure(x_range=[], y_range=[], plot_width=1200,plot_height=1200)

选择并加载数据源后(按下按钮),我这样做

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

并显示热图,但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会更新。我需要做些什么来更新空的FactorRanges?

编辑:

这是一个最低限度的工作示例。如果你将if True:更改为if False:并将p2 = figure(x_range=[], y_range=[], plot_width=1200,plot_height=1200)初始化为x_range.factors和y_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"
python bokeh
1个回答
1
投票

Bokeh版本0.13.0的升级解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.