散景从直方图中选择数据

时间:2019-04-25 01:38:41

标签: bokeh

是否可以与直方图进行交互以获得所选数据源的索引?

如果您有一个直方图来汇总箱中的所有数据值,并且想要从特定箱中获取选择的子集,是否可以通过与直方图的交互来做到这一点?

以下是显示示例的图像: Points associated with a target bin

从散点图中选择与目标仓关联的源数据索引比通过单击直方图仓来选择数据子集要困难得多。

以下是此示例的代码:

import numpy as np

from bokeh.layouts import gridplot
from bokeh.models import BoxSelectTool, LassoSelectTool
from bokeh.plotting import figure, curdoc

# create three normal population samples with different parameters
x1 = np.random.normal(loc=5.0, size=400) * 100;    y1 = np.random.normal(loc=10.0, size=400) * 10
x2 = np.random.normal(loc=5.0, size=800) * 50;     y2 = np.random.normal(loc=5.0, size=800) * 10
x3 = np.random.normal(loc=55.0, size=200) * 10;    y3 = np.random.normal(loc=4.0, size=200) * 10

x = np.concatenate((x1, x2, x3))
y = np.concatenate((y1, y2, y3))


TOOLS="pan,wheel_zoom,box_select,lasso_select,reset"

p = figure(tools=TOOLS, plot_width=600, plot_height=600, min_border=10, min_border_left=50,
           toolbar_location="above", x_axis_location=None, y_axis_location=None,
           title="Linked Histograms")

p.select(BoxSelectTool).select_every_mousemove = False
p.select(LassoSelectTool).select_every_mousemove = False
r = p.scatter(x, y, size=3, color="#3A5785", alpha=0.6)

# create the horizontal histogram
hhist, hedges = np.histogram(x, bins=20)
hzeros = np.zeros(len(hedges)-1)
hmax = max(hhist)*1.1

LINE_ARGS = dict(color="#3A5785", line_color=None)

ph = figure(toolbar_location=None, plot_width=p.plot_width, plot_height=200, x_range=p.x_range,
            y_range=(-hmax, hmax), min_border=10, min_border_left=50, y_axis_location="right")


ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, color="white", line_color="#3A5785")
hh1 = ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hzeros, alpha=0.5, **LINE_ARGS)


def update(attr, old, new):
    inds = new
    hhist1, _ = np.histogram(x[inds], bins=hedges)
    hh1.data_source.data["top"]   =  hhist1

r.data_source.selected.on_change('indices', update)


layout = gridplot([[p,ph]], merge_tools=False)
curdoc().add_root(layout)
curdoc().title = "Selection Histogram"



!powershell -command {'bokeh serve --show Histogram_brief-Copy1.ipynb'}

0 个答案:

没有答案