如何解决如何刷新散景中的人物?
我在 Bokeh 中有以下代码。它以红色显示四个点。用户可以从下拉列表中选择另一种颜色,单击“运行”,点将更改颜色。 使用此代码,点不会改变颜色,也不会更新 image_1.png。 关于如何刷新 image_1.png 并正确显示它的任何建议? 真的谢谢!
from bokeh.layouts import column,row
from bokeh.models import (Select,Button)
from bokeh.plotting import curdoc,figure
from bokeh.layouts import column,layout
import numpy as np
import matplotlib.pyplot as plt
x_range=(0,100)
y_range=(0,100)
color_1 = ['red','blue','yellow']
N = 4
x = np.random.random(size=N) * 10
y = np.random.random(size=N) * 10
radii = 15#np.random.random(size=N) * 15
line_color = Select(value='red',title='color:',width=200,options=color_1)
p = figure(tools="reset,pan,wheel_zoom,lasso_select")
file_name_1 = "test_sp/static/image_1.png"
def button_callback():
print('bc')
p = figure(tools="reset,lasso_select")
layout.children[1] = create_figure()
return p
def create_figure():
c1 = line_color.value
print(c1)
file_name_1 = "test_sp/static/image_1.png"
plt.scatter(x,y,radii,color=c1,alpha=0.6)
plt.savefig(file_name_1)
p = figure(tools="reset,lasso_select")
p.image_url(url=[file_name_1],x=x_range[0],y=y_range[1],w=x_range[1]-x_range[0],h=y_range[1]-y_range[0])
return p
button = Button(label='Run',width=100,button_type="success")
button.on_click(button_callback)
selects = column(line_color,button,width=420)
layout=row(selects,create_figure())
curdoc().add_root(layout)
curdoc().title = "color the dots"
编辑 1:我在此处发布的代码是一个更大代码的完整最小示例,我在其中更新了散景图(基于来自前端的用户输入,保存它然后刷新前端)。 这就是为什么我在前端有一个“运行”按钮(用于收集用户输入)并在 Bokeh 代码中有一个 create figure() 以允许我构建一个 Bokeh 图形(这是从 matplotlib 保存的 .png阴谋)。我仍然需要“运行”按钮和 create_figure()。我的问题是,即使 .png 在服务器端更新,我也无法使用此更新的 .png 刷新前端。您可能对如何刷新散景图有任何建议..?
解决方法
在您的代码中,您混合了 matplotlib
和 bokeh
。你必须解决这个问题。如果您想对散景使用回调,则必须使用 JavaScript
而不是 Python
回调。要编写您自己的回调,您必须编写此行 from bokeh.models import CustomJS
。但是没有 CustomJS
的情节也是可能的。
示例
from bokeh.layouts import row
from bokeh.models import Select
from bokeh.plotting import figure,show,output_notebook
output_notebook()
import numpy as np
color_1 = ['red','blue','green']
N = 4
x = np.random.randint(low=0,high=10,size=N)
y = np.random.randint(low=0,size=N)
p = figure(width=300,height=300)
points = p.scatter(x,y,fill_color='red',line_color=None)
select = Select(title="Color:",value="red",options=color_1,width=80)
select.js_link('value',points.glyph,'fill_color')
layout=row(select,p)
show(layout)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。