python-3.x - 是否可以从交互式散景图生成计算图/数据?
解决方案
此代码将百分比误差显示为理论点旁边的文本。您还可以在画布上从理论点到实验点画一条线,差异数据将显示在工具提示中。第一次单击(按钮释放)启动“差异”线。下一次单击结束它。工具提示始终保留在绘图上,但您可以轻松调整它,使其在第二次单击后消失。
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.events import *
source = ColumnDataSource({'x': [], 'y': []})
p = figure(plot_width = 900)
x_data = np.arange(10)
source_expected = dict(x = x_data, y = np.random.random(10), color=['blue']*10)
source_measured = dict(x = x_data, y = np.random.random(10), color=['grey']*10)
line_expected = p.line('x', 'y', line_color = 'blue', source = source_expected)
circle_expected = p.circle('x', 'y', color = 'color', source = source_expected, size = 10)
line_measured = p.line('x', 'y', line_color = 'grey', source = source_measured)
# circle_measured = p.circle('x', 'y', color = 'color', fill_color = 'white', source = source_measured, size = 60)
circle_measured = p.circle('x', 'y', color = 'color', source = source_measured, size = 10)
source_error = {'x': [], 'y': [], 'error': [], 'percent': [], 'text': [], 'color': []}
i = 0
for expected, measured in zip(source_expected['y'], source_measured['y']):
source_error['x'].append(x_data[i])
source_error['y'].append(measured)
error = expected - measured
percent = (error/expected) * 100
source_error['error'].append(error)
source_error['percent'].append(percent)
source_error['text'].append('+{}%'.format('%.2f' % percent) if percent > 0 else '{}%'.format('%.2f' % percent))
source_error['color'].append('green' if expected == measured else 'red')
i = i + 1
# text_measured = p.text('x', 'y', text = 'text', text_color = 'color', text_font_size = '9pt', x_offset = -25, y_offset = 6, source = source_error)
text_measured = p.text('x', 'y', text = 'text', text_color = 'color', text_font_size = '9pt', x_offset = 10, y_offset = 5, source = source_error)
line = p.line('x', 'y', line_color = 'red', line_dash = 'dashed', source = source)
callback_tap = '''
if (typeof custom_tooltip == 'undefined') {
custom_tooltip = document.createElement('div');
custom_tooltip.setAttribute('id','tooltip_div');
custom_tooltip.style = 'position: absolute; left: 0px; top: 0px; z-index: 9999; border:1px solid black; padding: 10px; background: white; font-family: arial; font-size: 12px'
document.body.prepend(custom_tooltip);
}
if (true === Bokeh.drawing) {
Bokeh.drawing = false
}
else {
if (!Bokeh.drawing) {
src.data = {'x':[], 'y':[]}
src.change.emit()
}
src.data['x'].push(cb_obj.x)
src.data['y'].push(cb_obj.y)
Bokeh.drawing = true
Bokeh.sx_start = cb_obj.sx
Bokeh.x_start = cb_obj.x
Bokeh.sy_start = cb_obj.sy
Bokeh.y_start = cb_obj.y
}'''
callback_mousemove = '''
function print(...args) {
for (i in args) {
console.log(args[i])
}
}
if (Bokeh.drawing) {
if (src.data['x'].length > 1) {
src.data['x'].pop()
src.data['y'].pop()
}
src.data['x'].push(cb_obj.x)
src.data['y'].push(cb_obj.y)
src.change.emit()
tooltip = document.getElementById('tooltip_div')
tooltip.style.left = cb_obj.sx + 30 + 'px'
tooltip.style.top = cb_obj.sy + 10 + 'px'
var error = Math.round((Bokeh.y_start - cb_obj.y) * 100) / 100
var percent = Math.round(error * 100 / Bokeh.y_start)
tooltip.innerHTML = 'Distance X: ' + Math.round(Bokeh.sx_start - cb_obj.sx) + ' px' + ' (' + (Math.round((Bokeh.x_start - cb_obj.x) * 100) / 100) + ' units)' +
'<br />' +
'Distance Y: ' + Math.round(Bokeh.sy_start - cb_obj.sy) + ' px' + ' (' + (Math.round((Bokeh.y_start - cb_obj.y) * 100) / 100) + ' units)' +
'<br />' +
'Error: ' + error + ' units' +
'<br />' +
'% Error: ' + percent + ' %'
}'''
p.js_on_event('tap', CustomJS(args = {'src': source, }, code = callback_tap))
p.js_on_event('mousemove', CustomJS(args = {'src': source, }, code = callback_mousemove))
show(p)
结果:
将注释行与它们旁边的行交换会使错误百分比显示在一个代表理论点的大圆圈中。它看起来像这样:
该脚本是为Python v3.7.2和Bokeh v1.3.0 编写的
推荐阅读
- php - 从该包中确定 Composer 包的当前安装版本
- c# - 通过 CreateFileA OPEN_EXISTING 参数更改属性后的 MS Access 安全警告
- python-3.x - 用漂亮的汤从 div 标签中刮取 href
- swift - Codable 和 CodingKeys
- django - 在 ListView 中使用两个模型
- python-3.x - 使用 Map() 时代码不打印集合
- unity3d - 物体撞击触发对撞机的接触点
- python - Django 在迁移期间无法导入本地模块
- python - 如何让程序不使用第一个选项
- sql - 将 querystring 值传递到 query1,然后将另一个值从 query1 传递到 query2