我有一个类Collection
,它包含一堆其他类对象Thing
,它们都具有不同值的相同属性。该方法绘制了值与所有收集到的对象的值Collection.plot(x, y)
的散点图,如下所示:x
y
Thing
from bokeh.plotting import figure, show
from bokeh.models import TapTool
class Thing:
def __init__(self, foo, bar, baz):
self.foo = foo
self.bar = bar
self.baz = baz
def plot(self):
# Plot all data for thing
fig = figure()
fig.circle([1,2,3], [self.foo, self.bar, self.baz])
return fig
class Collection:
def __init__(self, things):
self.things = things
def plot(self, x, y):
# Configure plot
title = '{} v {}'.format(x, y)
fig = figure(title=title, tools=['pan', 'tap'])
taptool = fig.select(type=TapTool)
taptool.callback = RUN_THING_PLOT_ON_CLICK()
# Plot data
xdata = [getattr(th, x) for th in self.things]
ydata = [getattr(th, y) for th in self.things]
fig.circle(xdata, ydata)
return fig
Thing
然后,我将绘制所有四个来源的“foo”与“baz”值的散点图:
A = Thing(2, 4, 6)
B = Thing(3, 6, 9)
C = Thing(7, 2, 5)
D = Thing(9, 2, 1)
X = Collection([A, B, C, D])
X.plot('foo', 'baz')
我希望在这里发生的是能够单击散点图上的每个点。单击时,它会运行plot
给定的方法,生成Thing
一个单独的图,其中包含所有 'foo'、'bar' 和 'baz' 值。
关于如何实现这一点的任何想法?
我知道我可以将所有对象的所有数据加载到 a 中ColumnDataSource
并使用这个玩具示例制作绘图,但在我的实际用例中,该Thing.plot
方法会进行很多复杂的计算,并且可能会绘制数千个点。我真的需要它来实际运行该Thing.plot
方法并绘制新图。这可行吗?
或者,我可以将所有预先绘制的图形Collection.plot
的列表传递给该方法,然后在点击时显示吗?Thing.plot
使用 Python>=3.6 和散景>=2.3.0。非常感谢你!