button - 如何刷新散景中的图形?
问题描述
我在散景中有以下代码。它以红色显示四个点。用户可以从下拉列表中选择另一种颜色,单击“运行”,点将更改颜色。使用此代码,点不会改变颜色,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,pan,wheel_zoom,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,pan,wheel_zoom,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, high=10, 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)
推荐阅读
- php - 安排会议算法
- c# - Unity 中带有 NAudio WaveOut 的 PlatformNotSupportedException
- r - 如何在 dplyr 中找到最小值和最大值?
- jenkins - 将 Kubernetes 与 Jenkins 管道连接起来
- javascript - 单击时更改变量的Javascript上下文菜单
- c# - 抛出异常:mscorlib.dll 中的“System.NotSupportedException”附加信息:不支持给定路径的格式
- azure - 如何在多域案例中与第 3 方单点登录 (SSO) IdP 进行 Azure 集成
- google-apps-script - 将 n 个 Google 电子表格文件合并到 n 个工作表中
- python - 如何处理“取消” - django allauth linkedin
- flutter - Flutter:“SharedPreferences”的实例