如何在Bokeh中链接(t,x)图和(x,y)图?

问题描述 投票:0回答:1

我正在测量两个变量x和y,它们是时间t的函数。我用Bokeh可视化这个,在x和y的散点图中作为t的函数,y的第三个散点图是x的函数。我希望缩放(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中显示的数据点?

python plot bokeh
1个回答
0
投票

您可以在此代码中添加额外的x轴(Bokeh v1.0.4)。或者,如果您愿意,可以在第3个图中使用时间作为主轴。

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)

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.