bokeh `figure.renderers` 的一个元素的 `_property_values` 如何直接改变?
How can the `_property_values` of an element of a bokeh `figure.renderers` be changed directly?
散景figure.renderers
的某个元素的_property_values
如何直接改变?我了解到 renderers
的元素有一个 id,所以我希望做类似 renderers['12345']
的事情。但因为它是一个列表(更准确地说是一个 PropertyValueList),所以这是行不通的。相反,我找到的唯一解决方案是遍历列表,将正确的元素存储在新指针(?)中,修改指针,从而修改原始元素。
这是我的玩具示例,其中直方图中的垂直线根据某些小部件的值进行更新:
import hvplot.pandas
import ipywidgets as widgets
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import Span
from bokeh.plotting import figure
%matplotlib inline
hist, edges = np.histogram([1, 2, 2])
p = figure()
r = p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:])
vline = Span(location=0, dimension='height')
p.renderers.extend([vline])
def update_hist(x):
myspan = [x for x in p.renderers if x.id==vline.id][0]
myspan._property_values['location'] = x
show(p, notebook_handle=True)
widgets.interact(update_hist, x = widgets.FloatSlider(min=1, max=2))
Bigreddot pointed me into the right direction: I don't have to update p
directly, but the elements used to generate p
(here the Span
). By this I found the 代码所在解决方案:更新vline.location
.
完整代码:
import hvplot.pandas
import ipywidgets as widgets
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import Span
from bokeh.plotting import figure
%matplotlib inline
hist, edges = np.histogram([1, 2, 2])
p = figure()
r = p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:])
vline = Span(location=0, dimension='height')
p.renderers.extend([vline])
show(p, notebook_handle=True)
def update_hist(x):
vline.location = x
push_notebook()
widgets.interact(update_hist, x = widgets.FloatSlider(min=1, max=2, step = 0.01))
作为一个 Python 初学者,我仍然经常监督 Python does not have variables。所以我们可以通过改变 y
.
来改变一个元素 x
x = ['alice']
y = x
y[0] = 'bob'
x # is now ['bob] too
散景figure.renderers
的某个元素的_property_values
如何直接改变?我了解到 renderers
的元素有一个 id,所以我希望做类似 renderers['12345']
的事情。但因为它是一个列表(更准确地说是一个 PropertyValueList),所以这是行不通的。相反,我找到的唯一解决方案是遍历列表,将正确的元素存储在新指针(?)中,修改指针,从而修改原始元素。
这是我的玩具示例,其中直方图中的垂直线根据某些小部件的值进行更新:
import hvplot.pandas
import ipywidgets as widgets
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import Span
from bokeh.plotting import figure
%matplotlib inline
hist, edges = np.histogram([1, 2, 2])
p = figure()
r = p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:])
vline = Span(location=0, dimension='height')
p.renderers.extend([vline])
def update_hist(x):
myspan = [x for x in p.renderers if x.id==vline.id][0]
myspan._property_values['location'] = x
show(p, notebook_handle=True)
widgets.interact(update_hist, x = widgets.FloatSlider(min=1, max=2))
Bigreddot pointed me into the right direction: I don't have to update p
directly, but the elements used to generate p
(here the Span
). By this I found the vline.location
.
完整代码:
import hvplot.pandas
import ipywidgets as widgets
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import Span
from bokeh.plotting import figure
%matplotlib inline
hist, edges = np.histogram([1, 2, 2])
p = figure()
r = p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:])
vline = Span(location=0, dimension='height')
p.renderers.extend([vline])
show(p, notebook_handle=True)
def update_hist(x):
vline.location = x
push_notebook()
widgets.interact(update_hist, x = widgets.FloatSlider(min=1, max=2, step = 0.01))
作为一个 Python 初学者,我仍然经常监督 Python does not have variables。所以我们可以通过改变 y
.
x
x = ['alice']
y = x
y[0] = 'bob'
x # is now ['bob] too