本文介绍了散景相当于matplotlib子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种创建包含多个子图的图的方法
I am looking for a way to create a plot the containing several subplots like
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
将在matplotlib中执行,然后可以通过ax0
和ax1
进行寻址.有没有办法在Bokeh中做类似的事情?在bokeh 示例画廊中,我只发现了一个地块.
would do in matplotlib, which then can be addressed by ax0
and ax1
. Is there a way to do something similar in Bokeh? In the bokeh examples gallery I only found single plots.
推荐答案
我认为您可以找到的更简单的示例是:
I think the simpler example you can find is:
import numpy as np
import bokeh.plotting as bk_plotting
import bokeh.models as bk_models
# for the ipython notebook
bk_plotting.output_notebook()
# a random dataset
data = bk_models.ColumnDataSource(data=dict(x=np.arange(10),
y1=np.random.randn(10),
y2=np.random.randn(10)))
# defining the range (I tried with start and end instead of sources and couldn't make it work)
x_range = bk_models.DataRange1d(sources=[data.columns('x')])
y_range = bk_models.DataRange1d(sources=[data.columns('y1', 'y2')])
# create the first plot, and add a the line plot of the column y1
p1 = bk_models.Plot(x_range=x_range,
y_range=y_range,
title="",
min_border=2,
plot_width=250,
plot_height=250)
p1.add_glyph(data,
bk_models.glyphs.Line(x='x',
y='y1',
line_color='black',
line_width=2))
# add the axes
xaxis = bk_models.LinearAxis()
p1.add_layout(xaxis, 'below')
yaxis = bk_models.LinearAxis()
p1.add_layout(yaxis, 'left')
# add the grid
p1.add_layout(bk_models.Grid(dimension=1, ticker=xaxis.ticker))
p1.add_layout(bk_models.Grid(dimension=0, ticker=yaxis.ticker))
# add the tools
p1.add_tools(bk_models.PreviewSaveTool())
# create the second plot, and add a the line plot of the column y2
p2 = bk_models.Plot(x_range=x_range,
y_range=y_range,
title="",
min_border=2,
plot_width=250,
plot_height=250)
p2.add_glyph(data,
bk_models.glyphs.Line(x='x',
y='y2',
line_color='black',
line_width=2))
# add the x axis
xaxis = bk_models.LinearAxis()
p2.add_layout(xaxis, 'below')
# add the grid
p2.add_layout(bk_models.Grid(dimension=1, ticker=xaxis.ticker))
p2.add_layout(bk_models.Grid(dimension=0, ticker=yaxis.ticker))
# add the tools again (it's only displayed if added to each chart)
p2.add_tools(bk_models.PreviewSaveTool())
# display both
gp = bk_plotting.GridPlot(children=[[p1, p2]])
bk_plotting.show(gp)
哪个生成输出:
这篇关于散景相当于matplotlib子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!