【问题标题】:How to link (t, x) plot and (x, y) plot in Bokeh?如何在散景中链接(t,x)图和(x,y)图?
【发布时间】:2019-08-11 22:39:50
【问题描述】:

我正在测量两个变量 x 和 y,作为时间 t 的函数。我正在用 Bokeh 将其可视化,在 x 和 y 的散点图中作为 t 的函数 (图 1,2)和作为 x 函数的 y 的第三个散点图(图 3)。我希望(x,y)图的缩放跟随前两个图的缩放。这就是我所拥有的:

import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
from bokeh.layouts import gridplot

t = pd.date_range('2018-01-01', periods=10, freq='H')
x = np.linspace(0, 5, len(t))
y = x**2
source = ColumnDataSource({'t': t, 'x': x, 'y': y})
tools = "pan, box_select, box_zoom, reset"


p1 = figure(tools=tools, x_axis_type='datetime')
p1.scatter(x='t', y='x', source=source)
p2 = figure(tools=tools, x_axis_type='datetime')
p2.x_range = p1.x_range
p2.scatter(x='t', y='x', source=source)
p3 = figure(tools=tools)
p3.scatter(x='x', y='y', source=source)
p = gridplot([[p1, p2, p3]])
show(p)

当放大 p1 时,p2 “跟随”(反之亦然)。有没有办法让 p3 也跟着,让 p3 只显示 p1 和 p2 中显示的数据点?

【问题讨论】:

  • 既然你的地块有一个共同的轴(时间't'),a) 为什么不把它们垂直排列在彼此的下方?这将使交叉选择在视觉上更加直观。或者 b) 你可以在同一个图上用不同的标记和颜色绘制 x,y (左侧 y 轴用于系列“x”,右侧 y 轴用于系列“y”)
  • 这里还有一个错误:您的各个子图 p1、p2、p3 有工具栏,但是当与gridplot 组合时,工具栏会消失。即使它应该有一个默认的merge_tools=True。我使用的是 1.4.0(我在 github.com/bokeh/bokeh/… 看不到任何相关问题)

标签: python plot selection bokeh interactive


【解决方案1】:

您可以像在这段代码中一样添加一个额外的 x 轴 (Bokeh v1.0.4)。或者,如果您愿意,您可以在第三个情节中使用时间作为主轴。

import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, LinearAxis, DatetimeTickFormatter
from bokeh.models.tickers import YearsTicker
from bokeh.layouts import gridplot

t = pd.date_range('2018-01-01', periods = 10, freq = 'H')
x = np.linspace(0, 5, len(t))
y = x ** 2
source = ColumnDataSource({'t': t, 'x': x, 'y': y})
tools = "pan,box_select,box_zoom,reset,wheel_zoom"

p1 = figure(tools = tools, x_axis_type = 'datetime', active_scroll = 'wheel_zoom')
p1.scatter(x = 't', y = 'x', source = source)

p2 = figure(tools = tools, x_axis_type = 'datetime')
p2.x_range = p1.x_range
p2.scatter(x = 't', y = 'x', source = source)

p3 = figure(tools = tools)
p3.extra_x_ranges = { "Time": p1.x_range }  # y_range_name = "Volume",
extra_x_axis = LinearAxis(x_range_name = "Time", ticker = YearsTicker())
extra_x_axis.formatter = DatetimeTickFormatter( days = ["%m/%d %H:%M"],
                                                months = ["%m/%d %H:%M"],
                                                hours = ["%m/%d %H:%M"],
                                                minutes = ["%m/%d %H:%M"])
p3.add_layout(extra_x_axis, place = 'below')
p3.scatter(x = 'x', y = 'y', x_range_name = "Time", source = source)

layout = gridplot([[p1, p2, p3]])

show(layout)

结果:

【讨论】:

    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2018-12-19
    相关资源
    最近更新 更多