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

时间:2019-03-21 05:27:56

标签: python plot bokeh

我正在测量两个变量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中显示的数据点?

1 个答案:

答案 0 :(得分:0)

您可以像此代码(Bokeh v1.0.4)中那样添加额外的x轴。或者,如果您愿意,可以在第三幅图中的主轴上使用时间。

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